在Python中使用BeautifulSoup进行web垃圾google搜索

2024-10-02 22:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

致力于一个自动化搜索几千个google搜索的项目,能够检查是否有“未找到结果”,并输入到一个数组中。你知道吗

使用BeautifulSoup,但我无法从URL导入HTML:

from bs4 import BeautifulSoup
import requests

html = requests.get('www.lifehack.org')
soup = BeautifulSoup(html,'html.parser')

软件包安装得很好,但我发现错误:

MissingSchema                             Traceback (most recent call last)
<ipython-input-28-8e881302fa25> in <module>
      1 from bs4 import BeautifulSoup
      2 import requests
----> 3 html = requests.get('www.lifehack.org')
      4 soup = BeautifulSoup(html,'html.parser')

C:\Program Files (x86)\Anaconda\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     73 
     74     kwargs.setdefault('allow_redirects', True)

+更多类似的东西

我不知道怎么解决这个问题。我希望能够快速得到的HTML直接进入程序,而不必复制它,并保存在一个本地HTML文件

任何帮助都将不胜感激,谢谢。你知道吗


Tags: infromorgimportparsergethtmlwww
1条回答
网友
1楼 · 发布于 2024-10-02 22:25:15

首先,您应该发布完整的错误消息,如果您只发布了部分错误消息,则无法解决问题。你知道吗

也就是说,有一件事可能会引起问题,那就是你的url需要完全限定。你知道吗

html = requests.get('http://www.lifehack.org')

事实上,如果您公布了执行代码时遇到的全部错误,您可能会看到这样的情况,这会给出您的答案:

MissingSchema: Invalid URL 'www.lifehack.org': No schema supplied. Perhaps you meant http://www.lifehack.org?

一旦解决了这个问题,就会遇到另一个问题:

Traceback (most recent call last):

File "", line 4, in soup = BeautifulSoup(html,'html.parser')

File "C:\bs4__init__.py", line 267, in init elif len(markup) <= 256 and (

TypeError: object of type 'Response' has no len()

您的html变量是一个响应对象,您不能将其直接传递给BeautifulSoup。您希望传递从响应中获得的文本。你知道吗

soup = BeautifulSoup(html.text,'html.parser')

这个故事的寓意是:注意你的错误信息,它们是你的向导。你知道吗

相关问题 更多 >