【问题标题】:Python: ConnectionError: 'Connection aborted' when scraping specific websitesPython:ConnectionError:抓取特定网站时“连接中止”
【发布时间】:2019-09-11 15:05:03
【问题描述】:

我正在尝试抓取此网站: https://www.footpatrol.com/

但是,该网站似乎拒绝了我的抓取尝试。

使用标题没有帮助。

from bs4 import BeautifulSoup
import requests

url = "https://www.footpatrol.com/"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
r = requests.get(url, headers = headers)
data = r.text
soup = BeautifulSoup(data, 'lxml')

for a in soup.find_all():
    print(a)

这导致我收到 ConnectionError,如何修复我的代码以便抓取网站?

【问题讨论】:

  • 你想要的输出是什么?
  • @DirtyBit 现在我只想要完整的源代码,剩下的我自己做
  • 您想要完整的源代码?好的。

标签: python web-scraping beautifulsoup python-requests screen-scraping


【解决方案1】:

我可以通过将用户代理更改为:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}

以下用户代理也可以使用:

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}

Chrome 版本似乎是你的 User Agent 的罪魁祸首。

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多