python爬取div内容
时间: 2023-09-20 07:11:15 浏览: 68
使用Python爬虫爬取div内容可以使用以下步骤:
1. 导入所需的库:requests和BeautifulSoup
```python
import requests
from bs4 import BeautifulSoup
```
2. 发送请求获取网页内容
```python
url = 'http://example.com'
response = requests.get(url)
html = response.content
```
3. 解析网页内容
```python
soup = BeautifulSoup(html, 'html.parser')
```
4. 找到想要的div标签
```python
div_tag = soup.find('div', {'class': 'example-class'})
```
其中,'class': 'example-class'是div标签的属性,可以根据实际情况修改。
5. 获取div标签的内容
```python
content = div_tag.text
```
完整代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
div_tag = soup.find('div', {'class': 'example-class'})
content = div_tag.text
print(content)
```
阅读全文