【发布时间】:2020-09-11 16:02:55
【问题描述】:
我有多个网站,我想获取每个网站的“联系我们”网址。网址不一定包含在所有网站的同一类中。但是,所有网站的innerHTML本质上都包含“联系人”这个词
如果 innerhtml 包含特定的单词,有没有办法从网页中提取 URL。 例如,在以下 HTML 的情况下,如果 innerhtml 包含单词“contact”(不区分大小写),我想提取 URL。
HTML = {
<a class="" style="COLOR: #000000; TEXT-DECORATION: none" href="http://www.candp.com/bin/index.asp?id=565B626C6C6A79504B575A4D626E" target=
"_parent">
<font size="2">
<strong>Contact Us</strong>
</font>
</a>
}
需要输出:-
'http://www.candp.com/bin/index.asp?id=565B626C6C6A79504B575A4D626E'
到目前为止,我可以访问以下代码,但它似乎不起作用:-
link=[]
driver.get(main_url)
elements = driver.find_elements_by_xpath("//a").get_attribute('href') # the href is not always contained in a tag
for el in elements:
if 'contact'.casefold() in str(el.text):
link.append(el.get_attribute('href'))
非常感谢任何帮助,
【问题讨论】:
标签: python-3.x selenium-webdriver web-scraping