Python脚本,该脚本将在迭代循环中使用引号搜索google
我正在尝试编写一个python脚本,它将在循环中搜索google并输出顶部链接。我目前有:Python脚本,该脚本将在迭代循环中使用引号搜索google,python,google-search,Python,Google Search,我正在尝试编写一个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/w
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条最聪明的狗”:dogs.com |。然后在每个循环后迭代1。我尝试过的这两种组合都失败了,我不知道如何让python忽略某些引号。为了让python忽略一个特殊字符,请事先添加一个反斜杠“\”: e、 g:“我说过”你好“应该这样写:“我说过”你好“ 此外,更具体地说。您还可以使用三个引号“”创建多行sring,在这种情况下,1引号不会结束字符串。如果使用此技术,需要记住的一条重要信息是,如果您希望以引号结束字符串,则必须始终使用反斜杠:
myStr = """ This is my string in which I wanted to say:
"hello\""""
为了使python忽略特殊字符,请事先添加反斜杠“\”: e、 g:“我说过”你好“应该这样写:“我说过”你好“ 此外,更具体地说,您还可以使用三个引号“”创建多行sring,在这种情况下,1引号不会结束字符串。如果使用此技术,需要记住的一条重要信息是,如果要以引号结束字符串,则必须使用反斜杠:
myStr = """ This is my string in which I wanted to say:
"hello\""""