谷歌抓取网页视频教程( 谷歌搜索googlesearch使用requests和BeautifulSoup4抓取Google搜索结果的用法 )

优采云 发布时间: 2022-01-24 10:09

  谷歌抓取网页视频教程(

谷歌搜索googlesearch使用requests和BeautifulSoup4抓取Google搜索结果的用法

)

  

  谷歌搜索

  googlesearch 是一个 Python 库,可以轻松抓取 Google 搜索结果。 googlesearch 使用请求和

  BeautifulSoup4 抓取 Google。

  安装

  要安装,请运行以下命令:

  python3 -m pip install googlesearch-python

或者

pip3 install googlesearch-python

  用法

  要获得搜索词的结果,只需使用 googlesearch 中的搜索功能。例如,要在 Google 中获得“Efficient Coders”的结果,只需运行以下程序:

  from googlesearch import search

import pprint

list = search("高效码农")

pprint.pprint(list)

  打印结果:

  ['https://www.xugj520.cn/',

'https://blog.csdn.net/Java_supermanNO1/article/details/104536476',

'https://weekly.manong.io/issues/295',

'https://item.jd.com/12481316.html',

'https://zhuanlan.zhihu.com/p/29481305',

'https://zhuanlan.zhihu.com/p/102597965',

'https://my.oschina.net/u/1189098',

'https://www.codercto.com/a/98794.html',

'https://www.cnblogs.com/huang0925/p/3612741.html',

'https://www.jiqizhixin.com/users/0d71c15b-b934-482a-aadc-5f5cf63afd60']

  其他选项

  googlesearch 支持其他几个选项。默认情况下,googlesearch 返回 10 个结果。这可以改变。例如,要在 Google 上获得 100 个结果,请运行以下程序。

  from googlesearch import search

import pprint

list = search("高效码农", num_results=100)

pprint.pprint(list)

  此外,您还可以更改 Google 搜索的语言。例如,要获得法语的结果,请运行以下程序:

  list = search("高效码农", num_results=100, lang='fr')

  完整的 API:

  googlesearch.search(str: term, int: num_results=10, str: lang="en") -> list

  代理设置

  修改 __init__.py 第 5 行:

  修改 __init__.py 第 15 行:

  response = get(google_url, headers=usr_agent, proxies=proxies)

  使用代码修改为:

  list = search("高效码农", proxies=proxies, num_results=100, lang='fr')

  完整代码为:

  from googlesearch import search

import pprint

proxies = {"http": '127.0.0.1:1087', "https": '127.0.0.1:1087', }

result = search("高效码农", proxies=proxies, num_results=100, lang='fr')

pprint.pprint(result)

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线