[英]BeautifulSoup can't crawl google search results?
嘗試抓取Google搜索結果。 我嘗試過,此代碼在所有其他網站上都可以很好地工作,但是不適用於google。 它返回一個空列表。
from BeautifulSoup import BeautifulSoup
import requests
def googlecrawler(search_term):
url="https://www.google.co.in/?gfe_rd=cr&ei=UVSeVZazLozC8gfU3oD4DQ&gws_rd=ssl#q="+search_term
junk_code=requests.get(url)
ok_code=junk_code.text
good_code=BeautifulSoup(ok_code)
best_code=good_code.findAll('h3',{'class':'r'})
print best_code
googlecrawler("healthkart")
它應該返回這樣的內容。
<h3 class="r"><a href="/url? sa=t&rct=j&q=&esrc=s&source=web&cd=6&cad=rja&uact=8&ved=0CEIQFjAF&url=http%3A%2F%2Fwww.coupondunia.in%2Fhealthkart&ei=qFmfVc2fFNO0uASti4PwDQ&usg=AFQjCNFHMzqn-rH4Hp-fZK0E4wwxJmevEg&sig2=QgwxMBdbPndyQTSH10dV2Q" onmousedown="return rwt(this,'','','','6','AFQjCNFHMzqn-rH4Hp-fZK0E4wwxJmevEg','QgwxMBdbPndyQTSH10dV2Q','0CEIQFjAF','','',event)" data-href="http://www.coupondunia.in/healthkart">HealthKart Coupons: July 2015 Coupon Codes</a></h3>
看着good_code
我根本看不到h3
或class "r"
。 這就是為什么您的代碼返回空列表的原因。
這樣的代碼就沒有問題,而是您所搜索的內容不存在。
您希望返回什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.