网页信息爬取
示例代码:
import requests from bs4 import BeautifulSoup def scrape_website(url): # 发起 GET 请求并获取网页内容 response = requests.get(url) # 检查响应状态码,200 表示请求成功 if response.status_code == 200: # 使用 BeautifulSoup 解析 HTML 内容 soup = BeautifulSoup(response.content, 'html.parser') # 在这里编写你的信息提取逻辑 # 例如,假设你想要提取所有段落文本内容 paragraphs = soup.find_all('p') # 打印提取的信息 for paragraph in paragraphs: print(paragraph.text.strip()) else: print("请求失败,状态码:", response.status_code) # 要爬取的网页 URL url = 'https://www.cnblogs.com/wenlong-4613615/p/18101331' # 调用函数进行网页信息爬取和打印 scrape_website(url)
标签:网页,url,BeautifulSoup,信息,爬取,response From: https://www.cnblogs.com/laixufei/p/18111245