lilililililili1
2021-06-15 00:02
采纳率: 0%
浏览 50

爬B站弹幕出现问题,求问大神

这个代码运行结束后不输出结果,但显示在PyCharm运行代码后无法输入也没有输出结果只显示Process finished with exit code 0,请问如何解决?已试过run那种方法好像不适用。

import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
import time
import pandas as pd

def get_bilibili_url(start,end):
    url_list = []
    data_list = [i for i in pd.date_range(start,end).strftime('%Y-%m-%d')]
    for date in data_list:
        url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=186803402&date={date}'
        url_list.append(url)
    return url_list


def get_bilibili_danmu(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
        "cookie": "_uuid=EBC95835-C50A-758C-B863-DA8945A860A494799infoc; buvid3=0ED54844-0EB0-41DC-9298-CC13FDA04E8B34762infoc; CURRENT_FNVAL=80; blackside_state=1; rpdid=|(u~)YJ|J)JY0J'uYkkJYYkJu; buvid_fp=0ED54844-0EB0-41DC-9298-CC13FDA04E8B34762infoc; SESSDATA=5c7a3865%2C1639139275%2C90c0e%2A61; bili_jct=d9d8355e156c1b06bbefdca5c863a63d; DedeUserID=276322917; DedeUserID__ckMd5=8e068e9c1acf6b20; sid=b1u62m3g; fingerprint=5f5d381848fb9c5d9172f71dc2540a19; buvid_fp_plain=DD91B0C4-82F9-436C-BE30-EAAF12AAC68D13451infoc; PVID=4; bsource=search_bing; bfe_id=1bad38f44e358ca77469025e0405c4a6"
    }
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    # print(response.status_code)
    soup = BeautifulSoup(response.text, 'lxml')
    datas = soup.find_all('d')
    file = open('后浪弹幕.txt', 'a')
    for data in datas:
        text = data.text
        print(text)
        file.write(text)
        file.write('\n')
    file.close()

def main():
    start = '2020-02-17'
    end = '2020-02-20'
    url_list = get_bilibili_url(start, end)
    # for url in url_list:
    #     print(url)
    with ThreadPoolExecutor(max_workers=1)as executor:
        executor.map(get_bilibili_danmu, url_list)


if __name__ == '__main__':
    main()

4条回答 默认 最新

相关推荐 更多相似问题