我正在尝试使用 python 进行谷歌搜索,它基本上是网络抓取。我正在使用特定短语进行搜索,并且期待详细的输出。我还想避免使用其他网络抓取模块,例如
bs4
(BeautifulSoup4) 或 urllib
。
我正在使用
googlesearch-python
模块,但它没有按预期工作:
from googlesearch import search
search("my search", advanced=True)
但我得到这个输出:
当我尝试使用 print() 函数时:
from googlesearch import search
print(search("my search", advanced=True))
它仍然返回:
(高级是一个参数,可以使搜索更详细并返回更多信息。)
有时,我也根本没有得到任何输出(这意味着程序立即结束,就好像其中有 0 行代码一样),而其他时候我得到生成器对象的输出。有没有办法使用
googlesearch-python
模块返回干净、详细的输出? (这个问题是我之前关于 googlesearch
模块的问题的后续问题:“Python google 搜索‘高级’参数意外”)
您将获得一个 Python 生成器。尝试将其转换为 List 来查看实际结果。
from googlesearch import search
# Convert the generator to a list and print the results
results = list(search("my search", num=10, advanced=True))
print(results)