鱼C论坛

 找回密码
 立即注册
查看: 1952|回复: 4

《极客Python之效率革命》中“找出最受欢迎编程课程”的问题

[复制链接]
发表于 2020-3-18 15:22:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
按照课程内老师的代码清单运行后,爬取到的记事本是空的,不知什么原因,请教各位鱼友,见下图
问题1.jpg
问题2.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-18 15:25:04 | 显示全部楼层
是不是忘记关闭文件了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-18 15:39:01 | 显示全部楼层
源码如下:
  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 = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
  12.     res = requests.get(url, headers=headers)

  13.     return res.text


  14. def get_datas(text):
  15.     soup = bs4.BeautifulSoup(text, "html.parser")
  16.    
  17.     datas = []  
  18.     videos = soup.find_all("li", class_="video matrix")
  19.     for video in videos:
  20.         # 获取标题
  21.         datas.append(video.a['title'])
  22.         # 获取URL
  23.         datas.append(video.a['href'])
  24.         # 获取观看数/弹幕数/上传时间/阿婆主
  25.         tags = video.select("div[class='tags'] > span")
  26.         for tag in tags:
  27.             datas.append(''.join(tag.text.split()))

  28.     return datas


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


  33. def main():
  34.     keyword, pages = get_input()
  35.     order = ['totalrank', 'click', 'dm', 'stow']
  36.     order_name = ['综合排序', '最多点击', '最多弹幕', '最多收藏']
  37.    
  38.     # 迭代每种排序
  39.     for i in range(4):
  40.         index = 1
  41.         # 迭代每一页
  42.         for page in range(1, pages+1):
  43.             url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&tids_1=36&page={}".format(keyword, order[i], page)
  44.             text = get_html(url)
  45.             datas = get_datas(text)
  46.             # 为每种排序创建一个文本文件单独存放
  47.             with open(order_name[i]+'.txt', 'a', encoding="utf-8") as file:
  48.                 for video_title, video_URL, video_watch, video_dm, video_time, video_up in grouped(datas, 6):
  49.                     file.write(' + '.join([str(index), video_title, video_URL, video_watch, video_dm, video_time, video_up, '\n']))
  50.                     index += 1
  51.             # 做一只善意的爬虫,不要给服务器带来负担
  52.             time.sleep(1)
  53.    
  54. if __name__ == "__main__":
  55.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-18 16:46:11 | 显示全部楼层
一个账号 发表于 2020-3-18 15:25
是不是忘记关闭文件了。。。

谢谢你的回答,但是我把记事本文档全部删除后再运行,生成的还是空文档。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 13:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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