鱼C论坛

 找回密码
 立即注册
查看: 2012|回复: 2

[作品展示] 爬取鱼C论坛问题求助板块

[复制链接]
发表于 2021-5-17 00:19:24 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 smog 于 2021-5-17 00:32 编辑
  1. import os

  2. import requests, time  # 数据请求
  3. import re  # 数据提取


  4. class FishC:
  5.     url = 'https://fishc.com.cn/bestanswer.php?mod=huzhu&type=undo&page=' # 鱼C论坛悬赏板块的url最后格式是 page=页数
  6.     headers = {
  7.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.56'}  # 设置请求头部
  8.     # 正则的设置请看下面的格式分析,分别提取 是否解决,标题,回答个数,提问时间
  9.     reg = '<font color=green>(.*?)</font>|<a href="(https://fishc.com.cn/thread-\d{6}-1-1.html)" target="_blank">(.*?)</a>|<font color="#999999">(\d+?)</font>|<font color="#999999">(2021-.*?)</font>'
  10.    
  11.     # 每一个问题的格式都是这样子的
  12.     # <tr>
  13.     #                         <td>
  14.     #                                                             <em>[<font color="green">待解决</font>]</em>
  15.     #                                                             <a href="https://fishc.com.cn/thread-196132-1-1.html" target="_blank">列表:用for循环出的一组数如何加入到一个空的列表中</a>
  16.     #                         </td>
  17.     #                         <td style="text-align:center;">
  18.     #                             <font color="#999999">1</font>
  19.     #                         </td>
  20.     #                         <td style="text-align:center;">
  21.     #                             <font color="#999999">2021-05-16 22:12</font>
  22.     #                         </td>
  23.     #</tr>
  24.    
  25.     def getHtml(self, url):
  26.         res = requests.get(url)
  27.         code = res.status_code
  28.         print(code)
  29.         if code == 200:
  30.             return res.text
  31.         else:
  32.             return None
  33.    
  34.     def run(self):
  35.         if not os.path.exists('data.csv'):
  36.             self.initial()
  37.         for i in range(11): # 爬个11页不要太多
  38.             url = self.url + str(i + 1)   # 获得完整的url
  39.             rawHtml = self.getHtml(url)
  40.             if rawHtml is not None:
  41.                 res = re.findall(self.reg, rawHtml)  
  42.                 res = [(res[i][0], res[i + 1][1], self.parse(res[i + 1][2]), res[i + 2][3], res[i + 3][4]) for i in
  43.                        range(len(res)) if
  44.                        i % 4 == 0]  # 将匹配的数据打包成一个个元组并生成一个list,这里建议自己打印输出看看(看看 res = re.findall(self.reg, rawHtml)  时res是啥再分析)
  45.                 with open('data.csv', 'a', encoding='utf-8') as fp:
  46.                     for j in res:
  47.                         for k in range(len(j)):
  48.                             fp.write(j[k])
  49.                             if k != len(j) - 1:
  50.                                 fp.write(',')
  51.                         fp.write('\n')
  52.                 print(len(res))  # 可以看到鱼C论坛每页都有15条数据
  53.                 print(res)

  54.             time.sleep(5)  # 设置请求间隔以免给鱼C论坛造成太大压力,不知道会不会封ip哈哈
  55.    

  56.     def parse(self, str):  # 这个是将html实体变为字符(其实可以省略)
  57.         str = re.sub('&lt;', '<', str)
  58.         str = re.sub('&gt;', '>', str)
  59.         return str
  60.    
  61.     def initial(self): # 初始化数据文件
  62.         dataHeads = ["是否解决", "链接", "标题", "回答数", "提问时间"]
  63.         with open('data.csv', 'w', encoding='utf-8') as fp:
  64.             for i in dataHeads:
  65.                 fp.write(i)
  66.                 if i != dataHeads[-1]:
  67.                     fp.write(',')
  68.             else:
  69.                 fp.write('\n')
  70.    
  71.        


  72.     def test(self):  # 这是自己调试时写的(先爬了一个页面分析正则),还是留下来吧,(可以忽略)
  73.         
  74.         if not os.path.exists('data.csv'):
  75.             self.initial()
  76.         
  77.         with open('raw.html', 'r', encoding='utf-8') as f:   # 这里打开html文件前要先爬一页数据,之前爬html保存到raw.html文件的代码被我删掉了。。。
  78.             txt = f.read()
  79.         
  80.         res = re.findall(self.reg, txt)
  81.         res = [(res[i][0], res[i + 1][1], self.parse(res[i + 1][2]), res[i + 2][3], res[i + 3][4]) for i in
  82.                range(len(res)) if
  83.                i % 4 == 0]
  84.         # res = [{"是否解决": i[0], "链接": i[1], "标题": self.parse(i[2]), "回答数": i[3], "提问时间": i[4]} for i in res]
  85.         with open('data.csv', 'a', encoding='utf-8') as fp:
  86.             for i in res:
  87.                 for j in range(len(i)):
  88.                     fp.write(i[j])
  89.                     if j != len(i) - 1:
  90.                         fp.write(',')
  91.                 fp.write('\n')
  92.         print(len(res))
  93.         print(res)
  94.    

  95. if __name__ == '__main__':
  96.     fishc = FishC()
  97.     fishc.run()
复制代码

每次请求输出格式:
200
15
[('待解决', 'https://fishc.com.cn/thread-196112-1-1.html', '操作符问题', '2', '2021-05-16 16:58'), ('待解决', 'https://fishc.com.cn/thread-196109-1-1.html', '碰到bug', '3', '2021-05-16 15:29'), ('待解决', 'https://fishc.com.cn/thread-196107-1-1.html', '初学者求救', '3', '2021-05-16 14:56'), ('待解决', 'https://fishc.com.cn/thread-196105-1-1.html', 'python作业谢谢', '7', '2021-05-16 14:51'), ('待解决', 'https://fishc.com.cn/thread-196104-1-1.html', '请教一下,怎么获取到验证码的请求地址呢', '0', '2021-05-16 14:48'), ('待解决', 'https://fishc.com.cn/thread-196102-1-1.html', '背景虚化问题 折磨了两个小时了', '2', '2021-05-16 14:21'), ('待解决', 'https://fishc.com.cn/thread-196101-1-1.html', 'LEGB原则', '1', '2021-05-16 14:01'), ('待解决', 'https://fishc.com.cn/thread-196099-1-1.html', '求助:python中怎么删除索引带单引号的列呢?', '3', '2021-05-16 13:36'), ('待解决', 'https://fishc.com.cn/thread-196098-1-1.html', 'KMP算法求助,大佬们帮忙看看代码有什么错', '0', '2021-05-16 12:50'), ('待解决', 'https://fishc.com.cn/thread-196097-1-1.html', '函数文档', '5', '2021-05-16 12:22'), ('待解决', 'https://fishc.com.cn/thread-196095-1-1.html', '老版本第20课课后题string1找字符问题', '7', '2021-05-16 11:10'), ('待解决', 'https://fishc.com.cn/thread-196080-1-1.html', 'c++ 高精度乘单精度', '1', '2021-05-15 23:30'), ('待解决', 'https://fishc.com.cn/thread-196078-1-1.html', 'position的一些问题', '2', '2021-05-15 22:55'), ('待解决', 'https://fishc.com.cn/thread-196077-1-1.html', '求助!!!求助!!!C语言', '2', '2021-05-15 22:51'), ('待解决', 'https://fishc.com.cn/thread-196076-1-1.html', 'notepad', '2', '2021-05-15 22:37')]

附上一个爬下来的文件地址:
文件地址
数据格式:
是否解决,链接,标题,回答数,提问时间
待解决,https://fishc.com.cn/thread-196130-1-1.html,计算嵌套列表某一层次的元素数量<新人求助>(可以的话麻烦看看我的代码怎么改),0,2021-05-16 21:41
待解决,https://fishc.com.cn/thread-196129-1-1.html,求助!代理服务器ip设计不成功,0,2021-05-16 21:36
待解决,https://fishc.com.cn/thread-196128-1-1.html,学不进去怎么办?,3,2021-05-16 21:16
待解决,https://fishc.com.cn/thread-196127-1-1.html,大佬们 linux怎么用制作windows的启动盘,4,2021-05-16 21:03
待解决,https://fishc.com.cn/thread-196125-1-1.html,有看不懂的报错,求救,2,2021-05-16 20:20
待解决,https://fishc.com.cn/thread-196123-1-1.html,python作业,刚学,求助,2,2021-05-16 20:03
...

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-17 09:23:58 | 显示全部楼层
我记得在哪里看过,不让爬本论坛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-18 00:12:07 | 显示全部楼层
南归 发表于 2021-5-17 09:23
我记得在哪里看过,不让爬本论坛

这样啊。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 17:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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