python爬虫多页
时间: 2023-08-25 18:17:37 浏览: 36
在Python中,你可以使用爬虫库(例如Requests、BeautifulSoup等)来爬取分页数据。面是一个示例代码,展示了如何使用Requests和BeautifulSoup来爬取分页数据:
```python
import requests
from bs4 import BeautifulSoup
def scrape_page(url):
# 发送HTTP GET请求
response = requests.get(url)
# 检查响应状态码
if response.status_code == 200:
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 提取需要的数据
data = soup.find_all('div', class_='item')
# 处理数据...
for item in data:
# 提取具体信息并进行处理
title = item.find('h2').text.strip()
link = item.find('a')['href']
print(title, link)
else:
print('请求失败')
# 循环爬取多页数据
for page in range(1, 11):
url = f'https://example.com/page/{page}'
scrape_page(url)
```
以上