编写一个爬虫程序,用来爬取斗鱼直播平台上所有房间标题和观众人数
时间: 2023-07-16 15:12:35 浏览: 297
Python爬虫实战--爬取斗鱼直播所有房间信息.zip
以下是Python实现的爬虫程序:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.douyu.com/directory/all"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
room_titles = soup.findAll("h3", {"class": "ellipsis"})
viewer_nums = soup.findAll("span", {"class": "dy-num fr"})
for i in range(len(room_titles)):
print("房间标题:", room_titles[i].text.strip())
print("观众人数:", viewer_nums[i].text.strip())
```
首先,我们发起GET请求来获取斗鱼直播平台的网页源代码。然后,使用BeautifulSoup库对页面进行解析,并找到所有的房间标题和观众人数。最后,使用循环将每个房间的标题和观众人数打印出来。
阅读全文