1

我正在尝试抓取这个网站:https ://www.senate.gov/general/contact_information/senators_cfm.cfm

我的代码:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

print(soup)

问题是它实际上并没有进入该站点。我在汤 var 中获得的 HTML 根本不是正确网页中的 HTML。

我不确定从这里去哪里!任何和所有的帮助将不胜感激。

4

2 回答 2

1

这对我有用

headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
r = requests.get(URL,headers=headers)

在这里找到信息 - https://towardsdatascience.com/5-strategies-to-write-unblock-able-web-scrapers-in-python-5e40c147bdaf

于 2020-05-29T14:13:04.060 回答
0

使用 python 请求模块时出现重复的 HTTP 503 错误

试试看:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.senate.gov/general/contact_information/senators_cfm.cfm'

page = requests.post(URL, headers=headers)

soup = BeautifulSoup(page.content, 'html.parser')
print(soup)
于 2020-05-29T14:16:45.450 回答