使用BeautifulSoup抓取网页

问题描述 投票:1回答:2

我正在尝试抓取此网站: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)

问题是它实际上并没有转到该站点。我从汤中获取的HTML根本与正确网页中的HTML完全不同。

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

python web-scraping beautifulsoup html-parsing web-scraping-language
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


0
投票

DUPLICATE HTTP 503 Error while using python requests module

尝试:

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)
© www.soinside.com 2019 - 2024. All rights reserved.