N
N
Nik Helnik2021-12-15 23:29:00
Python
Nik Helnik, 2021-12-15 23:29:00

How can I make .find() look for and retrieve data from the last element on the page?

For example, there are two elements on the page:
61ba50926646d526426986.jpeg
And with the help of .find() I need to find and pick up the div on the line with the number two (i.e. I need to find the very last div on the page). Preferably using the class in the tag, if it exists.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Shevkunov, 2021-12-17
@danila763

from bs4 import BeautifulSoup as bs
import requests as req

url = "https://url.ru"

headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.51 Safari/537.36'
}

headers = {'Accept'}
html = bs(req.get(url, headers=headers).text, 'lxml')
div = html.find_all('div', _class='class')[-1]
print(div)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question