403尝试使用Beautifulsoup时出现禁止信息

2024-10-01 07:40:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这已经在这里提到过了,我试图通过一个假的用户周界,但没有用。你能帮忙吗

import requests
from bs4 import BeautifulSoup

headers = requests.utils.default_headers()
headers.update({
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
})

page = requests.get('https://ingatlan.com/lista/elado+lakas')
soup = BeautifulSoup(page.content, 'html.parser')

print(soup.prettify())

错误消息如下所示:

$ python hello.py
<html>
 <head>
  <title>
   403 Forbidden
  </title>
 </head>
 <body>
  <center>
   <h1>
    403 Forbidden
   </h1>
  </center>
  <hr/>
  <center>
   nginx
  </center>
 </body>
</html>

Tags: 用户importtitlehtmlpagebodyh1requests
1条回答
网友
1楼 · 发布于 2024-10-01 07:40:28

它的反应很明显

import requests
from bs4 import BeautifulSoup

headers={
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
}
def main(url):
    with requests.Session() as req:
        req.headers.update(headers)
        r = req.get(url).text

        soup = BeautifulSoup(r, 'lxml')
        print(soup.prettify())

url = 'https://ingatlan.com/lista/elado+lakas'
main(url)

相关问题 更多 >