【发布时间】:2016-09-19 22:01:09
【问题描述】:
我正在使用此处的代码 (retrieve links from web page using python and BeautifulSoup) 来提取网站中的所有链接。
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request('http://www.bestwestern.com.au')
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
if link.has_attr('href'):
print link['href']
我正在使用这个网站http://www.bestwestern.com.au 作为测试。 不幸的是,我注意到代码没有提取一些链接,例如这个 http://www.bestwestern.com.au/about-us/careers/ 。我不知道为什么。 在页面的代码中,这是我发现的。
<li><a href="http://www.bestwestern.com.au/about-us/careers/">Careers</a></li>
我认为提取器通常应该识别它。 在 BeautifulSoup 文档中,我可以读到:“最常见的意外行为类型是您在文档中找不到您知道的标签。你看到它进去了,但是 find_all() 返回 [] 或 find() 返回 None。这是 Python 内置 HTML 解析器的另一个常见问题,它有时会跳过它不理解的标签。同样,解决方案是安装 lxml 或 html5lib。” 所以我安装了html5lib。但我仍然有同样的行为。
感谢您的帮助
【问题讨论】:
-
我实际上没有在此页面上看到“职业”链接 - 我们正在查看同一页面吗?..
-
您将通过查看此处的站点地图看到“职业”链接bestwestern.com.au/sitemap
标签: python-2.7 hyperlink beautifulsoup html5lib