python爬虫爬取网页数据
时间: 2023-09-02 12:15:27 浏览: 40
Python 爬虫可以使用多种库和框架来实现,比如 Requests、BeautifulSoup、Scrapy 等。
首先,使用 Requests 库发送 HTTP 请求,获取网页内容。然后,可以使用 BeautifulSoup 库对网页进行解析和提取需要的数据。如果需要爬取多个网页,可以使用 Scrapy 框架编写爬虫程序,实现自动爬取和数据存储。
下面是一个简单的爬虫示例代码,可以获取指定网页的标题和正文:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string
content = soup.find('div', class_='content').get_text()
print('Title:', title)
print('Content:', content)
```
需要注意的是,爬虫需要遵守网站的 robots.txt 文件中规定的爬取限制,不得进行恶意爬取和攻击行为。同时,需要考虑网站的反爬虫机制,比如设置请求头、使用代理等方式来减少被封禁的风险。