鱼C论坛

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

我的第一个爬虫!!!!!开心

[复制链接]
发表于 2019-10-7 22:02:39 | 显示全部楼层 |阅读模式

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

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

x
给大佬们看看
还没学正则,就全部用的字符串
爬取妹子图还是算了吧,毕竟我是正[yi]人[guan]君[qin]子[shou],所以就爬了鱼哥的帖子(别打我)

问一下,那些特殊符号大家都是怎么处理的??
  1. import urllib.request as ur

  2. illegal = {'\xa0':' ', '<br />':'', '<div class="quote">':'', '<blockquote>':'"', '</blockquote>':'"','</div>':'', '<div>':'', '&nbsp;':' ', '0xaf':''}
  3. data = []



  4. def find_comment(content):
  5.     global data
  6.     account = []
  7.     comments = []
  8.     head = content.find('<div id="postlist" class="pl bm">')
  9.     tail = content.find('<div id="postlistreply" class="pl">', head)
  10.     p = content.find('<div id="post_', head, tail)
  11.     while p != -1:
  12.            t = content.find('<div id="post_', p + 1, tail)
  13.            temp = content.find('class="xw1"', p + 1, t)
  14.            temp = content.find('>', temp + 1, t)
  15.            temp_t = content.find('</a>', temp + 1, t)
  16.            name = content[temp+1:temp_t]
  17.            account.append(name)
  18.            temp = content.find('<td class="t_f" id="postmessage_', temp_t + 1, t) + 40
  19.            temp_t = content.find('</td>', temp + 1, t)
  20.            comment = content[temp:temp_t]
  21.            comments.append(comment)
  22.            p = content.find('<div id="post_', t + 1, tail)
  23.     num = len(account)
  24.     for i in range(0, num):
  25.         data.append([account[i], comments[i]])

  26. def filt():
  27.     global data
  28.     for each in data:
  29.         if '<strong>回复' in each[1]:
  30.             t = each[1].find('#', 111)
  31.             repeated_num = each[1][111:t]
  32.             t1 = each[1].find('<i>', t) + 3
  33.             t2 = each[1].find('</i>', t1)
  34.             repeated_name = each[1][t1:t2]
  35.             t = each[1].find('</strong>', t) + 9
  36.             each[1] = '回复了' + repeated_num + '楼用户' + repeated_name + '的评论  ' + each[1][t:]
  37.         for i in illegal:
  38.             while i in each[1]:
  39.                 each[1] = each[1].replace(i, illegal[i])
  40.         while '<img src=' in each[1]:
  41.             p = each[1].find('<img src=')
  42.             t = each[1].find('/>', p) + 2
  43.             each[1] = each[1][:p] + '<图片表情>' + each[1][t:]
  44.   
  45. def save_comment(data, page_p):
  46.     global f
  47.     f.writelines('='*20 + '第%d页'%page_p + '='*20 + '\n\n')
  48.     for each in data:
  49.         f.writelines('用户名:' + each[0] + ' 评论道' + '\n' + each[1] + '\n\n')
  50.     print('第%d页评论已爬取完毕'%page_p)
  51.     return

  52. def get_page(page_p):
  53.     url_head = 'https://fishc.com.cn/thread-1053-'
  54.     url_tail = '-1.html'
  55.     page_url = url_head + str(page_p) + url_tail
  56.     response = ur.urlopen(page_url)
  57.     content = response.read()
  58.     content = content.decode('gbk', 'ignore')
  59.     return content

  60. f = open('comments.txt','w')
  61. page_start = int(input("请输入需要爬取起始页数:"))
  62. page_end = int(input("请输入需要爬取终止页数:"))
  63. page_p = page_start
  64. while page_p <= page_end:
  65.     content = get_page(page_p)
  66.     find_comment(content)
  67.     filt()
  68.     save_comment(data, page_p)
  69.     page_p += 1
  70. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-14 17:18:15 | 显示全部楼层
是否注释下内容   好让我们新人 观摩学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-14 20:35:03 | 显示全部楼层
jy02618370 发表于 2019-10-14 17:18
是否注释下内容   好让我们新人 观摩学习

来啦来啦
第一次当大佬
其实可以用正则避免很多问题,但是当时还没学正则
以下:
  1. import urllib.request as ur

  2. illegal = {'\xa0':' ', '<br />':'', '<div class="quote">':'', '<blockquote>':'"', '</blockquote>':'"','</div>':'', '<div>':'', '&nbsp;':' ', '0xaf':''}
  3. #这里保存非法字符。因为网站编码问题,不能被转码为ASCII码,会报错,所以做特殊处理
  4. data = []
  5. #data列表用于保存某一页的 用户和评论


  6. def find_comment(content):
  7.     #find__comment函数用于查找评论, 将(用户名称,评论内容)存入全局列表data
  8.     global data
  9.     account = []#用户名列表
  10.     comments = []#评论内容列表
  11.     #对网站URL分析得知,评论的URL部分目录为:postlist/postlistreply/post_编号
  12.     head = content.find('<div id="postlist" class="pl bm">')
  13.     tail = content.find('<div id="postlistreply" class="pl">', head)
  14.     p = content.find('<div id="post_', head, tail)
  15.     while p != -1:
  16.         #当找不到评论时跳出循环
  17.            t = content.find('<div id="post_', p + 1, tail)
  18.            temp = content.find('class="xw1"', p + 1, t)
  19.            temp = content.find('>', temp + 1, t)
  20.            temp_t = content.find('</a>', temp + 1, t)
  21.            name = content[temp+1:temp_t]
  22.            account.append(name)
  23.            #记录用户名
  24.            temp = content.find('<td class="t_f" id="postmessage_', temp_t + 1, t) + 40
  25.            temp_t = content.find('</td>', temp + 1, t)
  26.            comment = content[temp:temp_t]
  27.            comments.append(comment)
  28.            #记录评论内容
  29.            p = content.find('<div id="post_', t + 1, tail)
  30.            #找到下一个post_编号标签
  31.     num = len(account)
  32.     for i in range(0, num):
  33.         data.append([account[i], comments[i]])
  34.         #存储到全局列表data中

  35. def filt():
  36.     #应为filter(过滤器),但是python对filter函数已有定义,故名为filt。用于滤去非法字符
  37.     global data
  38.     for each in data:
  39.         if '<strong>回复' in each[1]:
  40.             #如果该评论是回复,则修改为回复的格式:某人 回复了 某楼用户 某某 的评论:评论内容
  41.             t = each[1].find('#', 111)
  42.             repeated_num = each[1][111:t]
  43.             t1 = each[1].find('<i>', t) + 3
  44.             t2 = each[1].find('</i>', t1)
  45.             repeated_name = each[1][t1:t2]
  46.             t = each[1].find('</strong>', t) + 9
  47.             each[1] = '回复了' + repeated_num + '楼用户' + repeated_name + '的评论  ' + each[1][t:]
  48.         for i in illegal:
  49.             while i in each[1]:
  50.                 each[1] = each[1].replace(i, illegal[i])
  51.         while '<img src=' in each[1]:
  52.             #如果评论中有图片,则将图片替换为<图片表情>
  53.             p = each[1].find('<img src=')
  54.             t = each[1].find('/>', p) + 2
  55.             each[1] = each[1][:p] + '<图片表情>' + each[1][t:]
  56.   
  57. def save_comment(data, page_p):
  58.     #save_comment用于保存评论
  59.     global f
  60.     f.writelines('='*20 + '第%d页'%page_p + '='*20 + '\n\n')
  61.     for each in data:
  62.         f.writelines('用户名:' + each[0] + ' 评论道' + '\n' + each[1] + '\n\n')
  63.     print('第%d页评论已爬取完毕'%page_p)
  64.     return

  65. def get_page(page_p):
  66.     #get_page用于寻找下一页的URL
  67.     url_head = 'https://fishc.com.cn/thread-1053-'
  68.     url_tail = '-1.html'
  69.     page_url = url_head + str(page_p) + url_tail
  70.     response = ur.urlopen(page_url)
  71.     content = response.read()
  72.     content = content.decode('gbk', 'ignore')
  73.     return content

  74. f = open('comments.txt','w')
  75. page_start = int(input("请输入需要爬取起始页数:"))
  76. page_end = int(input("请输入需要爬取终止页数:"))
  77. page_p = page_start
  78. while page_p <= page_end:
  79.     #读取页面→寻找评论→过滤→保存→去往下一页
  80.     content = get_page(page_p)
  81.     find_comment(content)
  82.     filt()
  83.     save_comment(data, page_p)
  84.     page_p += 1
  85. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 16:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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