python爬虫爬取网页数据
时间: 2023-09-03 12:06:39 浏览: 33
Python是一种非常流行的编程语言,可以用它来编写爬虫程序。下面是一个简单的Python爬虫示例:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com"
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
# 获取网页标题
title = soup.title.string
print(title)
# 获取所有链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))
```
上述代码使用requests库向指定网址发送GET请求,然后使用BeautifulSoup库解析网页内容。通过soup对象可以获取网页标题、链接等内容。当然,爬虫还需要考虑反爬机制、数据存储等问题,这里只是一个简单的示例。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)