Python/Selenium-如何遍历< li>中的href? [英] Python/Selenium - how to loop through hrefs in <li>?
本文介绍了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)
这篇关于Python/Selenium-如何遍历< li>中的href?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文