所以我在網路抓取時從一個css選擇器中得到了一個字串輸出,這個字串有7行,其中6行是無用的,我只想要第4行。
字串如下:
?????:
??,???
???:
????
????? ????:
???? ???
? ??? ??? : ???,???,??? ?????
是否有辦法只列印第4行?
爬行代碼是什么?
抓取代碼:
color = driver.find_elements_by_css_selector("div[class='col']"/span>)
for c in color。
print(c.text)
uj5u.com熱心網友回復:
是的,當然了! 查看關于串列項的python檔案
color = driver.find_elements_by_css_selector("div[class='col']"/span>)
print(color[3].text)
List items are indexed, the first item has index [0], the second item has index 1等等。
uj5u.com熱心網友回復:
我不確定我對這個問題的理解是否正確,但是假設你有一個多行的字串,一個解決方案可以是:
string = ''this string
存在于
在多個
行
所以讓我們選擇
一行
''
def select_line(string, line_index)。
return string.splitlines()[line_index]
result = select_line(string,3)
print(result)
這個函式將選擇你想要的數字行(索引0是第一行)
uj5u.com熱心網友回復:
如果你想要的是1到4的專案,可以試試這個:
for idx in range(4)。
print(color[idx].text)
如果你只想要第4個,可以試試這個。(在Python中索引在list中從zero開始。)
print(color[3].text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/307841.html
標籤:
