python爬虫爬取网页数据
时间: 2023-10-06 14:14:47 浏览: 44
Python爬虫可以使用第三方库比如Requests和BeautifulSoup等来爬取网页数据。
下面是一个简单的示例,使用Requests和BeautifulSoup库来爬取网页数据:
```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
print('网页标题为:', title)
# 获取网页正文
content = soup.body.text
print('网页正文为:', content)
```
在这个示例中,我们使用Requests库来发送HTTP请求,获取网页数据。然后使用BeautifulSoup库来解析网页数据,并提取出网页标题和正文。
相关问题
python爬虫爬取网页并保存
答案:可以使用Python中的requests和BeautifulSoup库来爬取网页内容,并使用Python中的文件操作函数将爬取到的数据保存到本地文件中。具体实现方法可以参考相关的Python爬虫教程。
阅读全文
相关推荐










