import requests
from bs4 import BeautifulSoup
# 使用了requests库和BeautifulSoup库,请求网页并获取响应内容,然后使用BeautifulSoup对网页进行解析,最后通过遍历问题列表,打印问题和答案。
# 请求网页并获取响应内容
url = 'https://www.ceshi.com'
response = requests.get(url)
html_content = response.content
# 使用BeautifulSoup解析网页
soup = BeautifulSoup(html_content, 'html.parser')
# 找到所有的问题列表
questions = soup.find_all('div', class_='question')
# 遍历问题列表,并打印问题和答案
for question in questions:
q = question.find('h3').text
a = question.find('p').text
print('Question:', q)
print('Answer:', a)
print()
python requests 写爬虫的基本代码
最新推荐文章于 2024-03-25 13:55:15 发布