我要的是爬取这个网页的名称、集数、描述
时间: 2024-01-24 11:05:17 浏览: 26
好的,这里是修改后的代码,可以爬取该网页的名称、集数、描述:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://v.qq.com/channel/tv?channel=tv&feature=7&iarea=814&listpage=1'
response = requests.get(url)
html = response.content.decode('utf-8')
soup = BeautifulSoup(html, 'html.parser')
for item in soup.select('.list_item'):
title = item.select_one('.figure_title a').get_text().strip()
link = item.select_one('.figure_title a')['href']
episode = item.select_one('.figure_desc').get_text().strip()
desc = item.select_one('.figure_desc + .figure_info').get_text().strip()
print(title, episode, desc)
```
在原来的代码基础上,我们新增了两个变量`episode`和`desc`,分别用于提取电视节目的集数和描述。在HTML结构中,集数和描述都在`figure_desc`元素下面,而描述是`figure_desc`元素的兄弟元素,所以我们使用了`select_one`方法的相邻兄弟选择器`+`来获取描述元素。运行代码后,就可以输出名称、集数和描述信息了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)