您可以使用 google 提供的更程序化的 api 来获取结果,而不是尝试屏幕抓取人工搜索界面,没有错误检查或断言,这符合所有 google 条款和条件,建议您查看使用此的详细信息网址:
import requests
def search(query, pages=4, rsz=8):
url = 'https://ajax.googleapis.com/ajax/services/search/web'
params = {
'v': 1.0, # Version
'q': query, # Query string
'rsz': rsz, # Result set size - max 8
}
for s in range(0, pages*rsz+1, rsz):
params['start'] = s
r = requests.get(url, params=params)
for result in r.json()['responseData']['results']:
yield result
例如获得 200 个与“google”相关的结果:
>>> list(search('google', pages=24, rsz=8))
[{'GsearchResultClass': 'GwebSearch',
'cacheUrl': 'http://www.google.com/search?q=cache:y14FcUQOGl4J:www.google.com',
'content': 'Search the world's information, including webpages, images, videos and more. \n<b>Google</b> has many special features to help you find exactly what you're looking\xa0...',
'title': '<b>Google</b>',
'titleNoFormatting': 'Google',
'unescapedUrl': 'https://www.google.com/',
'url': 'https://www.google.com/',
'visibleUrl': 'www.google.com'},
...
]
要使用 Google 的自定义搜索 API,您需要注册为开发人员。您每天可以获得 100 个免费查询(我不确定这是 API 调用还是它允许对同一查询进行分页算作 1 个查询):
您可以使用requests进行查询:
import requests
url = 'https://www.googleapis.com/customsearch/v1'
params = {
'key': '<key>',
'cx': '<cse reference>',
'q': '<search>',
'num': 10,
'start': 1
}
resp = requests.get(url, params=params)
results = resp.json()['items']
使用start,您可以进行与上述类似的分页。
还有很多其他可用参数,您可以查看 CSE 的 REST 文档:https://developers.google.com/custom-search/json-api/v1/reference/cse/list#request
Google 也有一个客户端 API 库:pip install google-api-python-client 你也可以使用:
from googleapiclient import discovery
service = discovery.build('customsearch', 'v1', developerKey='<key>')
params = {
'q': '<query>',
'cx': '<cse reference>',
'num': 10,
'start': 1
}
query = service.cse().list(**params)
results = query.execute()['items']