Python-数据爬取(爬虫)简介
    		       		warning:
    		            这篇文章距离上次修改已过434天,其中的内容可能已经有所变动。
    		        
        		                
                
import requests
from bs4 import BeautifulSoup
 
# 目标URL
url = 'https://www.example.com/some_page'
 
# 发送HTTP请求
response = requests.get(url)
 
# 检查请求是否成功
if response.status_code == 200:
    # 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 提取页面上的数据
    # 假设我们要提取所有的段落文本
    paragraphs = soup.find_all('p')
    for p in paragraphs:
        print(p.get_text())
else:
    print(f"请求页面失败,状态码: {response.status_code}")
 
# 注意:实际应用中需要处理网络请求中的异常和反爬虫策略。这段代码演示了如何使用Python的requests库发送HTTP GET请求,以及如何使用BeautifulSoup库解析HTML页面并提取所需数据。在实际应用中,你需要根据目标网站的结构和数据位置调整选择器和提取逻辑。
评论已关闭