0

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)
1
  • Which variable contains the string? Commented Sep 15, 2021 at 10:59

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was trying 'enumerate' and I got confused. It really helped, Thank you!
1

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)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.