【发布时间】:2020-05-24 09:08:52
【问题描述】:
我有以下 Python 代码:
req=requests.get("https://pythonhow.com/example.html")
content=req.content
soup=BeautifulSoup(content, "html.parser")
all=soup.find_all(attrs={"class": "cities"})
当我将此 URL 粘贴到浏览器中时,我得到了预期的标记结构:
<body data-gr-c-s-loaded="true" cz-shortcut-listen="true">
<h1 align="center"> Here are three big cities </h1>
<div class="cities">
<h2>London</h2>
<p>London is the capital of England and it's been a British settlement since 2000 years ago. </p>
</div>
<div class="cities">
<h2>Paris</h2>
<p>Paris is the capital city of France. It was declared capital since 508.</p>
</div>
<div class="cities">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan and one of the most populated cities in the world.</p>
</div>
</body>
然而,当我检查 requests.get 内容时,我有以下 HTML:
<head>
<title>Not Acceptable!</title>
</head>
<body>
<h1>Not Acceptable!</h1>
<p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p>
</body>
为什么requests 获取的内容与我的浏览器不同?我怀疑它与某些请求标头有关,但我不知道从哪里开始。
【问题讨论】:
标签: python python-3.x http beautifulsoup python-requests