我使用此處的代碼(retrieve links from web page using python and BeautifulSoup)從網站中提取所有鏈接。無法找到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。但我仍然有同樣的行爲。
謝謝您的幫助
我實際上沒有看到「招聘」鏈接此頁面上 - 我們尋找到相同的頁面.. – alecxe
你會看到「職業生涯」的鏈接通過查看網站地圖在這裏HTTP:// WWW? .bestwestern.com.au/sitemap/ – BND