爬取帖子出错 Invalid URL '#': No scheme supplied. Perhaps you meant https://#?
时间: 2024-02-13 19:59:41 浏览: 42
这个错误提示是因为在获取帖子链接时,有些网页中的链接是相对路径,而不是完整的URL地址。这种情况下,requests库会默认在链接前加上当前页面的URL,导致链接变成了无效的URL。
解决这个问题的方法是在获取帖子链接时,判断链接是否为完整的URL地址,如果不是,需要加上网站的域名部分,将其转换成完整的URL地址。可以使用urljoin方法实现这个功能,例如:
```python
from urllib.parse import urljoin
link = article.find("a")["href"]
if not link.startswith("http"):
link = urljoin(url, link) # 将相对路径转换成完整的URL地址
```
在这里,我们引入了urllib库中的urljoin方法,将相对路径转换成完整的URL地址,其中url参数是当前页面的URL地址。然后,我们可以使用转换后的链接来访问帖子详情页面,获取帖子的内容。
相关问题
Invalid URL '127.0.0.1/mask/': No scheme supplied. Perhaps you meant http://127.0.0.1/mask/?
Yes, it is possible that the correct URL is http://127.0.0.1/mask/. The error message suggests that the URL provided is missing the scheme (http or https), which is necessary for the browser to understand how to connect to the website. Adding the scheme to the URL should resolve the issue.
报错信息是 f"Invalid URL {url!r}: No scheme supplied. " requests.exceptions.MissingSchema: Invalid URL 'None': No scheme supplied. Perhaps you meant https://None?
这个报错信息意味着在使用 requests 库发送 HTTP 请求时,传入的 URL 参数是无效的。具体来说,报错信息指出没有提供有效的 URL 方案(scheme),并且推测你可能意图使用 https,但实际上传入的 URL 是 None。
要解决这个问题,你需要检查传入的 URL 参数是否正确,并确保它包含有效的 URL 方案。例如,如果你想要使用 https 方案,正确的 URL 格式应该是类似于 "https://example.com" 的形式。