gpt4 book ai didi

Python 网页抓取 Zacks 网站错误 : [WinError 10054] An existing connection was forcibly closed by the remote host

转载 作者:行者123 更新时间:2023-12-01 01:12:43 25 4
gpt4 key购买 nike

我想获取此页面上的数据: https://www.zacks.com/stock/quote/MA

我尝试使用 Python 中的 Beautiful Soup 执行此操作,但收到错误:“[WinError 10054] 现有连接被远程主机强制关闭”。

有人可以指导我吗?

from bs4 import BeautifulSoup
import urllib
import re
import urllib.request

url = 'https://www.zacks.com/stock/quote/MA'

r = urllib.request.urlopen(url).read()
soup = BeautifulSoup(r, "lxml")
soup

谢谢!

最佳答案

该网站正在阻止您的请求,可能主机不允许没有请求 header 的请求。您可以尝试使用 Selenium 包模拟“真实”请求。

这正在工作:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup




options = Options()

options.set_headless(headless=True)

url = 'https://www.zacks.com/stock/quote/MA'

browser = webdriver.Firefox(firefox_options=options)

browser.get(url)

html_source = browser.page_source

soup = BeautifulSoup(html_source, "lxml")

print(soup)

browser.close()

关于Python 网页抓取 Zacks 网站错误 : [WinError 10054] An existing connection was forcibly closed by the remote host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54687304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com