python爬取虎牙直播弹幕
时间: 2023-10-02 14:05:56 浏览: 66
你可以使用Python来爬取虎牙直播的弹幕。以下是一个简单的示例代码:
```python
import requests
def get_danmaku(room_id):
url = f"https://www.huya.com/{room_id}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
danmaku_json = response.json()["data"]["info"]["0"]["content"]
for danmaku in danmaku_json:
print(danmaku["content"])
else:
print("获取弹幕失败")
room_id = "your_room_id" # 替换为你想要爬取的直播间ID
get_danmaku(room_id)
```
请注意,这个示例代码只是一个简单的演示,具体的实现可能会有所不同。另外,爬取直播弹幕可能涉及到一些法律和道德问题,请确保你的行为符合相关规定。