Python 使用BeautifulSoup搜索谷歌搜索结果
我的目标是使用BeautifulSoup在网上搜索谷歌搜索结果。我使用anacondapython并使用Ipython作为IDE控制台。为什么在运行以下命令时不获取输出Python 使用BeautifulSoup搜索谷歌搜索结果,python,web-scraping,beautifulsoup,information-retrieval,Python,Web Scraping,Beautifulsoup,Information Retrieval,我的目标是使用BeautifulSoup在网上搜索谷歌搜索结果。我使用anacondapython并使用Ipython作为IDE控制台。为什么在运行以下命令时不获取输出 def google_scrape(query): address = "http://www.google.com/search?q=%s&num=100&hl=en&start=0" % (urllib.quote_plus(query)) request = urllib2.Requ
def google_scrape(query):
address = "http://www.google.com/search?q=%s&num=100&hl=en&start=0" % (urllib.quote_plus(query))
request = urllib2.Request(address, None, {'User-Agent':'Mosilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'})
urlfile = urllib2.urlopen(request)
page = urlfile.read()
soup = BeautifulSoup(page)
linkdictionary = {}
for li in soup.findAll('li', attrs={'class':'g'}):
sLink = li.find('a')
print sLink['href']
sSpan = li.find('span', attrs={'class':'st'})
print sSpan
return linkdictionary
if __name__ == '__main__':
links = google_scrape('english')
您从未向linkedDictionary添加任何内容
def google_scrape(query):
address = "http://www.google.com/search?q=%s&num=100&hl=en&start=0" % (urllib.quote_plus(query))
request = urllib2.Request(address, None, {'User-Agent':'Mosilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'})
urlfile = urllib2.urlopen(request)
page = urlfile.read()
soup = BeautifulSoup(page)
linkdictionary = {}
for li in soup.findAll('li', attrs={'class':'g'}):
sLink = li.find('a')
sSpan = li.find('span', attrs={'class':'st'})
linkeDictionary['href'] = sLink['href']
linkedDictionary['sSpan'] = sSpan
return linkdictionary
if __name__ == '__main__':
links = google_scrape('english')
我应该添加什么呢?无论你想返回什么,我都想在网上搜索“英语”这个词的谷歌搜索结果。你能告诉我怎么编辑这个代码吗?