准备工作:
需要安装requests和beautifulsoup4库。
pip install requests
pip install beautifulsoup4
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取百度首页的HTML内�??
url = 'https://www.baidu.com/'
response = requests.get(url)
# 检查�?�求�?否成�?
if response.status_code == 200:
# 使用BeautifulSoup解析HTML内�??
soup = BeautifulSoup(response.text, 'html.parser')
# 打印整个页面的HTML
print(soup.prettify())
else:
print('Failed to retrieve the webpage')
成功抓取到网页数据: