【发布时间】:2019-06-24 09:12:04
【问题描述】:
我正在尝试编写一个 python 脚本,它将在循环中搜索 google 并输出顶部链接。我目前有这个:
import urllib
import json as m_json
for x in range(3, 5):
query = 'x mile run'
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url'] # was URL in the original and that threw a name error exception
print ( title + '; ' + url )
我的问题是修改搜索输入以接受双引号,因为我希望脚本在特定网站中搜索字符串。例如,我希望能够搜索:|“#1 Smartest Dog”:dogs.com|。然后在每个循环之后迭代那个 1 。我尝试过的这种组合非常失败,我不确定如何让 python 忽略某些引号。
【问题讨论】:
标签: python google-search