python - 在网站上使用 python Beautiful Soup 时,不断收到此错误 : urllib. error.HTTPError: HTTP Error 403: Forbidden

标签 python html beautifulsoup

这是我用来获取耐克服装数据的代码。

import urllib.request

#Base url for website
url = 'http://store.nike.com/us/en_us/pw/mens-clothing/1mdZ7pu?ipp=120'

# A lot of sites don't like the user agents of Python 3, so I specify one here
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()

然后错误看起来像这样:
urllib.error.HTTPError:HTTP 错误 403:禁止

如何打开并解析此 HTML 页面?

最佳答案

或者尝试selenium webdriver。

from selenium import webdriver
from bs4 import BeautifulSoup as bs

browser = webdriver.Firefox()
url = 'http://store.nike.com/us/en_us/pw/mens-clothing/1mdZ7pu?ipp=120'
browser.get(url)
source = browser.page_source
soup = bs(source, "html.parser")
print(soup)

这对我有用,尽管我只是个新手:)

关于python - 在网站上使用 python Beautiful Soup 时,不断收到此错误 : urllib. error.HTTPError: HTTP Error 403: Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44705023/

相关文章:

python - 获取列表中给定项目的计数(不使用 numpy)

Python 单元测试 : Automatically Running the Debugger when a test fails

javascript - 选择特定选项后禁用 select2 上的选项

html - 悬停时顶部导航栏下拉菜单消失得太快 React

python - 如何测试美丽汤对象的类型?

python 从href源中提取id值

python - 在 Pandas dataframe.to_csv 中设置缓冲区 0

javascript - 将按钮更改为图像导航 slider Javascript

python - 组合列表中的项目,直到找到包含特定文本的项目?

python - PyCharm 中类 'objects' 的未解析属性引用 'Foo'