這是我第一次爬網,我試圖爬網,只有網頁(1 -...),以下網站:無法檢索<a>標記href(以「?」代替http/s開頭)使用beautifulsoup
http://jobs.monster.com/search/?q=data%20science
使用python beautifulsoup,但現在看來似乎是無法識別的廣告代碼的網頁。 標籤看起來是這樣的:
<a href="?q=data-science&page=1" class="page-link">1</a>
和我的部分代碼如下所示:
import urlparse
import urllib
import re
from bs4 import BeautifulSoup
# start with this page
url = "http://jobs.monster.com/search/?q=data%20science"
#http://jobs.monster.com/search/?q=data%20science&page=2
# stack of urls from nytimes
urls = [url]
#print urls
# visited urls
visited = [url]
while len(urls) > 0:
try:
htmltext = urllib.urlopen(urls[0]).read()
except:
print urls[0]
soup = BeautifulSoup(htmltext)
urls.pop(0)
for tag in soup.find_all('a', {'class':'page-link'}):
print tag
我沒有得到任何錯誤,但沒有什麼是打印出來要麼...我猜那是因爲href不是以http/s開頭的?
任何人都可以幫忙嗎?
感謝
您試圖抓取的頁面可能是由javascript動態生成的。 BeautifulSoup和urllib都不執行javascript,所以它看到的頁面沒有鏈接。將您正在獲取的htmltext轉儲到本地文件中,並查看它實際獲得的內容。 – RobertB