梦回极光 发表于 2020-3-3 09:51:25

对【爬虫篇】《极客Python之效率革命》 找出b站最受欢迎视频的一些改动

对小甲鱼大佬发布的 找出b站最受欢迎的编程课程 的爬虫代码的改进。b站网页源代码有所变化,title的class从class_="video matrix"变成了class_="video-item matrix"。
把对编程区的搜索改成了综合搜索。
另改进了生成的txt文本的排版。
import requests
import bs4
import time

def get_input():
    keyword = input("请输入关键词:")
    pages = int(input("请输入要爬取得页数(1~50):"))

    while pages not in range(1, 51):
      pages = int(input("请输入正确的页数:"))

    return keyword, pages


def get_html(url):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
    res = requests.get(url, headers=headers)

    return res.text



def get_datas(text):
    soup = bs4.BeautifulSoup(text, "html.parser")
   
    datas = []
    videos = soup.find_all("li", class_="video-item matrix")
    for video in videos:
      # 获取标题
      datas.append(video.a['title'])
      # 获取URL
      datas.append('https:'+video.a['href'])       #多出//
      # 获取观看数/弹幕数/上传时间/阿婆主
      tags = video.select("div > span")
      for tag in tags:
            datas.append(''.join(tag.text.split()))

    return datas

   


def grouped(iterable, n):
    "将列表切成n片一组"
    "s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
    return zip(**n)


def main():
    keyword, pages = get_input()
    order = ['totalrank', 'click', 'dm', 'stow']
    order_name = ['综合排序', '最多点击', '最多弹幕', '最多收藏']
   
    # 迭代每种排序
    for i in range(4):
      index = 1
      # 迭代每一页
      for page in range(1, pages+1):
            url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&tids_1=0&page={}".format(keyword, order, page)
            text = get_html(url)
            datas = get_datas(text)
            # 为每种排序创建一个文本文件单独存放
            with open(keyword+"   "+order_name+'.txt', 'a', encoding="utf-8") as file:
                for video_title, video_URL, video_watch, video_dm, video_time, video_up in grouped(datas, 6):
                  file.write('       '.join())
                  index += 1
            # 做一只善意的爬虫,不要给服务器带来负担
            time.sleep(1)
   
if __name__ == "__main__":
    main()不过这段代码在我的电脑上,第一次打开时无法运行的,需要二次打开才能正常工作。很魔性。。。。

老兵hb 发表于 2020-7-14 22:25:21

andy_william 发表于 2020-4-14 19:34
Traceback (most recent call last):
File "D:\python\搜索B站编程课程.py", line 1, in
    import r ...

没有安装requests模块

费小牛 发表于 2020-7-9 18:25:08

第34行是什么意思 ,小白求教

ai自由 发表于 2020-7-9 09:09:00

andy_william 发表于 2020-4-14 19:34
Traceback (most recent call last):
File "D:\python\搜索B站编程课程.py", line 1, in
    import r ...

什么情况   大佬我也是这问题

ai自由 发表于 2020-7-9 09:08:24

============= RESTART: D:/A桌面/Users/XY/Desktop/python练习代码/爬取编程课.py =============
Traceback (most recent call last):
File "D:/A桌面/Users/XY/Desktop/python练习代码/爬取编程课.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
>>>

不知道什么情况谢谢各位大佬

oooipussy 发表于 2020-6-20 17:49:08

第一次 不行是什么bug?

andy_william 发表于 2020-4-14 19:34:04

Traceback (most recent call last):
File "D:\python\搜索B站编程课程.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

大佬,这个是什么情况啊

starchaser 发表于 2020-3-7 06:17:32

牛批

杜若左 发表于 2020-3-6 07:51:20

谢谢大佬{:10_256:}
页: [1]
查看完整版本: 对【爬虫篇】《极客Python之效率革命》 找出b站最受欢迎视频的一些改动