python里面的request库和BeautifulSoup怎么下载
时间: 2024-04-05 10:08:16 浏览: 15
1. 安装 requests 和 BeautifulSoup 库:
```
pip install requests
pip install beautifulsoup4
```
2. 在 Python 脚本中导入库:
```python
import requests
from bs4 import BeautifulSoup
```
3. 使用 requests 库下载网页:
```python
url = 'https://www.example.com'
response = requests.get(url)
```
4. 使用 BeautifulSoup 库解析网页:
```python
soup = BeautifulSoup(response.content, 'html.parser')
```
5. 从解析后的网页中提取需要的内容:
```python
titles = soup.find_all('h1')
for title in titles:
print(title.text)
```
6. 完整代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
titles = soup.find_all('h1')
for title in titles:
print(title.text)
```