使用Python爬虫抓取网页的核心步骤如下:1. 安装必要库通过pip安装核心库:pip install requests beautifulsoup4requests:负责发送HTTP请求获取网页内容beautifulsoup4:解析HTML文档并提取数据2. 发送HTTP请求import requestsurl = "https://example.com"response = requests.get(url)# 检查请求是否成功(状态码200)response.raise_for_status()关键点:通过response.content获取二进制内容,response.text获取解码后的文本3. 解析HTML文档from bs4 import BeautifulSoupsoup = BeautifulSoup(response.content, "html.parser")# 也可使用更快的解析器(需安装):# soup = BeautifulSoup(response.content, "lxml")解析器选择:html.parser(Python内置)lxml(需单独安装,速度更快)html5lib(最宽松的解析方式)4. 数据提取方法基础提取示例:# 获取标题title = soup.title.string# 获取所有链接links = [a['href'] for a in soup.find_all('a', href=True)]# 获取特定class元素items = soup.find_all('div', class_='item')高级选择器:# CSS选择器first_paragraph = soup.select_one('div.content p')all_headings = soup.select('h1, h2, h3')# 属性过滤images = soup.find_all('img', src=lambda x: x and 'logo' in x)5. 数据处理与存储# 存储到文件with open('links.txt', 'w') as f: for link in links: f.write(f"{link}n")# 转换为JSONimport jsondata = {'title': title, 'links': links}with open('data.json', 'w') as f: json.dump(data, f)完整实战示例import requestsfrom bs4 import BeautifulSoupdef scrape_website(url): try: # 1. 获取页面 headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get(url, headers=headers, timeout=10) response.raise_for_status() # 2. 解析内容 soup = BeautifulSoup(response.text, 'lxml') # 3. 提取数据 result = { 'title': soup.title.string if soup.title else 'No title', 'headings': [h.text.strip() for h in soup.find_all(['h1', 'h2'])], 'links': list(set( a['href'] for a in soup.find_all('a', href=True) if not a['href'].startswith('#') )) } return result except Exception as e: print(f"Error scraping {url}: {str(e)}") return None# 使用示例data = scrape_website("https://example.com")if data: print(f"Title: {data['title']}") print(f"Found {len(data['links'])} unique links")关键注意事项请求头设置:添加User-Agent避免被识别为爬虫异常处理:网络请求和解析都可能出错遵守规则:检查robots.txt(如https://example.com/robots.txt)设置合理的请求间隔(建议1-3秒)性能优化:使用Session()对象保持连接对大规模爬取考虑异步请求(aiohttp)扩展建议动态内容处理:对于JavaScript渲染的页面,可使用Selenium或Playwright数据清洗:提取后建议用re模块或pandas处理脏数据反爬策略:遇到验证码时可考虑第三方服务(如2Captcha)通过以上步骤,您可以系统化地完成网页数据抓取任务。实际开发中建议将各功能模块化,并添加日志记录和重试机制以提高稳定性。



































