python - 特定网站不允许我从他们的页面获取链接

标签 python python-3.x beautifulsoup

您好,似乎可以在大多数网页上使用,但我不明白为什么它不会带回该网站的链接。有谁知道为什么?查看源码,有这样的内容: 它应该带回“a href="https://thebillingtongroup.com/food-group/billington-foodservice/”。

import httplib2
from bs4 import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('https://thebillingtongroup.com/')

for link in BeautifulSoup(response, 'html.parser', parse_only=SoupStrainer('a')):
    if link.has_attr('href'):
        print(link['href'])

最佳答案

您需要使用请求 header :

headers = {'User-Agent': 'Mozilla/5.0'}

http = httplib2.Http()
status, response = http.request('https://thebillingtongroup.com/', headers=headers)
...

某些网站需要有效的User-Agent来处理请求。

关于python - 特定网站不允许我从他们的页面获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422550/

相关文章:

python - 在Python中检查( "and", "or")优先级的简单方法

python - 为什么多线程和不同的函数/范围共享单个导入过程

python - 为数据分配编号

相同unicode的python不同长度

python - 如何在python中访问xml文档中的Beautifulsoup保留字?

python - 如何在 BeautifulSoup 中使用元素的样式定义(例如填充、字体大小等)来抓取元素

python - 使用 Python 进行抓取。无法获取想要的数据

python - 如何在 ubuntu 上安装开放库?

python - 将轴线添加到 matplotlib 图中

python - 缺少1个必需的位置参数-财务