鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 5

[已解决]Python爬虫

[复制链接]
发表于 2022-3-23 16:22:26 | 显示全部楼层
建议把你的代码放上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-23 17:49:32 | 显示全部楼层    本楼为最佳答案   

有一个地方的标签的 class 改了:
  1. import requests
  2. import bs4
  3. import time


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

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

  9.     return keyword, pages


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

  15.     return res.text


  16. def get_datas(text):
  17.     soup = bs4.BeautifulSoup(text, "html.parser")

  18.     datas = []
  19.     #videos = soup.find_all("li", class_="video matrix")                 
  20.     videos = soup.find_all("li", class_="video-item matrix")               # 这里的标签类名改了
  21.     for video in videos:
  22.         # 获取标题
  23.         datas.append(video.a['title'])
  24.         # 获取URL
  25.         datas.append(video.a['href'])
  26.         # 获取观看数/弹幕数/上传时间/阿婆主
  27.         tags = video.select("div[class='tags'] > span")

  28.         for tag in tags:
  29.             datas.append(''.join(tag.text.split()))
  30.     return datas


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



  35. def main():
  36.     keyword, pages = get_input()
  37.     order = ['totalrank', 'click', 'dm', 'stow']
  38.     order_name = ['综合排序', '最多点击', '最多弹幕', '最多收藏']

  39.     # 迭代每种排序
  40.     for i in range(4):
  41.         index = 1
  42.         # 迭代每一页
  43.         for page in range(1, pages + 1):
  44.             # url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&tids_1=36&page={}".format(keyword,
  45.             #                                                                                                 order[i],
  46.             #                                                                                                 page)
  47.             url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&page={}".format(keyword,
  48.                                                                                                   order[i],
  49.                                                                                                   page)

  50.             text = get_html(url)
  51.             datas = get_datas(text)
  52.             # 为每种排序创建一个文本文件单独存放
  53.    
  54.             with open(order_name[i] + '.txt', 'a', encoding="utf-8") as file:
  55.                 for video_title, video_URL, video_watch, video_dm, video_time, video_up in grouped(datas, 6):
  56.                     file.write(' + '.join(
  57.                         [str(index), video_title, video_URL, video_watch, video_dm, video_time, video_up, '\n']))
  58.                     index += 1
  59.             # 做一只善意的爬虫,不要给服务器带来负担
  60.             time.sleep(1)


  61. if __name__ == "__main__":
  62.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-5 06:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表