Python/Selenium-如何遍历< li>中的href?
问题描述:
网址: https://www.ipsos.com/en-us/knowledge/society/covid19-research-in-unertain-times
大家好,我想按如下方式解析HTML:
Hi folks, I want to parse the HTML as below:
我想在<中获得所有href. li>元素和突出显示的文本.我尝试了代码
I want to get all hrefs within the < li > elements and the highlighted text. I tried the code
elementList = driver.find_element_by_class_name('block-wysiwyg').find_elements_by_tag_name("li")
for i in range(len(elementList)):
driver.find_element_by_class_name('blcokwysiwyg').find_elements_by_tag_name("li").get_attribute("href")
但是该区块没有返回任何内容.
But the block returned none.
任何人都可以通过上述代码为我提供帮助吗?谢谢您的帮助!
Can anyone please help me with the above code? Thank you for the help!
答
我想它将为您获取所需的内容.
I suppose it will fetch you the required content.
import requests
from bs4 import BeautifulSoup
link = 'https://www.ipsos.com/en-us/knowledge/society/covid19-research-in-uncertain-times'
r = requests.get(link)
soup = BeautifulSoup(r.text,"html.parser")
for item in soup.select(".block-wysiwyg li"):
item_text = item.get_text(strip=True)
item_link = item.select_one("a[href]").get("href")
print(item_text,item_link)