【问题标题】:url open returns errors on some websites and not othersurl open 在某些网站而不是其他网站上返回错误
【发布时间】:2020-08-16 02:35:35
【问题描述】:

我正在尝试使用 BeautifulSoup 来收集一些数据,在查看示例时,一些网站可以正常工作,而另一些则不能。这是我遇到错误的示例代码。

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
page_url = "https://www.basspro.com/shop/en"
uClient = uReq(page_url)

这会返回一大堆错误,包括“return opener.open(url, data, timeout)”

但是当我将 URL 更改为 page_url = "https://store.google.com/" 它工作正常。

有人可以帮我理解发生了什么吗?

【问题讨论】:

    标签: python python-3.x beautifulsoup python-requests


    【解决方案1】:

    您需要随请求一起发送适当的标头。发送用户代理。

    import requests
    
    page_url = "https://www.basspro.com/shop/en"
    
    headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}
    
    res = requests.get(page_url, headers=headers)
    
    print(res.status_code)
    

    输出:

    200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 2011-03-25
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多