So I got a string output out of a css selector while web crawling and the string has 7 lines, 6 of them are useless and I only want the 4th line.
The string is as follows:
کارکرد:
۵۰,۰۰۰
رنگ:
سفید
وضعیت بدنه:
بدون رنگ
قیمت صفر : ۳۱۵,۰۰۰,۰۰۰ تومان
Is there a way to print only the 4th line?
The crawling code:
color = driver.find_elements_by_css_selector("div[class='col']")
for c in color:
print(c.text)
CodePudding user response:
Yes, of course! See python documentation about list items
color = driver.find_elements_by_css_selector("div[class='col']")
print(color[3].text)
List items are indexed, the first item has index [0], the second item has index 1 etc.
CodePudding user response:
I'm not sure if i understand the problem correctly, but assuming you have a string with multiple lines, a solution could be:
string = '''this string
exists
on multiple
lines
so lets pick
a line
'''
def select_line(string, line_index):
return string.splitlines()[line_index]
result = select_line(string,3)
print(result)
This function would select the number line you want (index 0 being the first line)
CodePudding user response:
If you want items one
to four
try this:
for idx in range(4):
print(color[idx].text)
And if you want only 4th
try this: (in python index in list
start from zero
.)
print(color[3].text)