楼主: fantuanxiaot
6770 39

[源码分享] [转载]基于Python的爬虫代码 [推广有奖]

已卖:1597份资源

大师

9%

还不是VIP/贵宾

-

威望
7
论坛币
-234454 个
通用积分
225.8477
学术水平
3783 点
热心指数
3819 点
信用等级
3454 点
经验
150360 点
帖子
7597
精华
32
在线时间
1329 小时
注册时间
2013-2-4
最后登录
2025-3-23

初级学术勋章 初级热心勋章 中级热心勋章 中级学术勋章 初级信用勋章 中级信用勋章 高级热心勋章 高级学术勋章 特级学术勋章 特级热心勋章 高级信用勋章 特级信用勋章

楼主
fantuanxiaot 发表于 2015-2-6 22:51:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

基于python的百度贴吧爬虫代码





本帖隐藏的内容


  1.             # -*- coding: utf-8 -*-
  2. #---------------------------------------
  3. #   程序:百度贴吧爬虫
  4. #   版本:0.5
  5. #   作者:why
  6. #   日期:2013-05-16
  7. #   语言:Python 2.7
  8. #   操作:输入网址后自动只看楼主并保存到本地文件
  9. #   功能:将楼主发布的内容打包txt存储到本地。
  10. #---------------------------------------
  11.   
  12. import string
  13. import urllib2
  14. import re

  15. #----------- 处理页面上的各种标签 -----------
  16. class HTML_Tool:
  17.     # 用非 贪婪模式 匹配 \t 或者 \n 或者 空格 或者 超链接 或者 图片
  18.     BgnCharToNoneRex = re.compile("(\t|\n| |<a.*?>|<img.*?>)")
  19.      
  20.     # 用非 贪婪模式 匹配 任意<>标签
  21.     EndCharToNoneRex = re.compile("<.*?>")

  22.     # 用非 贪婪模式 匹配 任意<p>标签
  23.     BgnPartRex = re.compile("<p.*?>")
  24.     CharToNewLineRex = re.compile("(<br/>|</p>|<tr>|<div>|</div>)")
  25.     CharToNextTabRex = re.compile("<td>")

  26.     # 将一些html的符号实体转变为原始符号
  27.     replaceTab = [("<","<"),(">",">"),("&","&"),("&","\""),(" "," ")]
  28.      
  29.     def Replace_Char(self,x):
  30.         x = self.BgnCharToNoneRex.sub("",x)
  31.         x = self.BgnPartRex.sub("\n    ",x)
  32.         x = self.CharToNewLineRex.sub("\n",x)
  33.         x = self.CharToNextTabRex.sub("\t",x)
  34.         x = self.EndCharToNoneRex.sub("",x)

  35.         for t in self.replaceTab:
  36.             x = x.replace(t[0],t[1])
  37.         return x
  38.      
  39. class Baidu_Spider:
  40.     # 申明相关的属性
  41.     def __init__(self,url):
  42.         self.myUrl = url + '?see_lz=1'
  43.         self.datas = []
  44.         self.myTool = HTML_Tool()
  45.         print u'已经启动百度贴吧爬虫,咔嚓咔嚓'
  46.    
  47.     # 初始化加载页面并将其转码储存
  48.     def baidu_tieba(self):
  49.         # 读取页面的原始信息并将其从gbk转码
  50.         myPage = urllib2.urlopen(self.myUrl).read().decode("gbk")
  51.         # 计算楼主发布内容一共有多少页
  52.         endPage = self.page_counter(myPage)
  53.         # 获取该帖的标题
  54.         title = self.find_title(myPage)
  55.         print u'文章名称:' + title
  56.         # 获取最终的数据
  57.         self.save_data(self.myUrl,title,endPage)

  58.     #用来计算一共有多少页
  59.     def page_counter(self,myPage):
  60.         # 匹配 "共有<span class="red">12</span>页" 来获取一共有多少页
  61.         myMatch = re.search(r'class="red">(\d+?)</span>', myPage, re.S)
  62.         if myMatch:
  63.             endPage = int(myMatch.group(1))
  64.             print u'爬虫报告:发现楼主共有%d页的原创内容' % endPage
  65.         else:
  66.             endPage = 0
  67.             print u'爬虫报告:无法计算楼主发布内容有多少页!'
  68.         return endPage

  69.     # 用来寻找该帖的标题
  70.     def find_title(self,myPage):
  71.         # 匹配 <h1 class="core_title_txt" title="">xxxxxxxxxx</h1> 找出标题
  72.         myMatch = re.search(r'<h1.*?>(.*?)</h1>', myPage, re.S)
  73.         title = u'暂无标题'
  74.         if myMatch:
  75.             title  = myMatch.group(1)
  76.         else:
  77.             print u'爬虫报告:无法加载文章标题!'
  78.         # 文件名不能包含以下字符: \ / : * ? " < > |
  79.         title = title.replace('\\','').replace('/','').replace(':','').replace('*','').replace('?','').replace('"','').replace('>','').replace('<','').replace('|','')
  80.         return title


  81.     # 用来存储楼主发布的内容
  82.     def save_data(self,url,title,endPage):
  83.         # 加载页面数据到数组中
  84.         self.get_data(url,endPage)
  85.         # 打开本地文件
  86.         f = open(title+'.txt','w+')
  87.         f.writelines(self.datas)
  88.         f.close()
  89.         print u'爬虫报告:文件已下载到本地并打包成txt文件'
  90.         print u'请按任意键退出...'
  91.         raw_input();

  92.     # 获取页面源码并将其存储到数组中
  93.     def get_data(self,url,endPage):
  94.         url = url + '&pn='
  95.         for i in range(1,endPage+1):
  96.             print u'爬虫报告:爬虫%d号正在加载中...' % i
  97.             myPage = urllib2.urlopen(url + str(i)).read()
  98.             # 将myPage中的html代码处理并存储到datas里面
  99.             self.deal_data(myPage.decode('gbk'))
  100.             

  101.     # 将内容从页面代码中抠出来
  102.     def deal_data(self,myPage):
  103.         myItems = re.findall('id="post_content.*?>(.*?)</div>',myPage,re.S)
  104.         for item in myItems:
  105.             data = self.myTool.Replace_Char(item.replace("\n","").encode('gbk'))
  106.             self.datas.append(data+'\n')



  107. #-------- 程序入口处 ------------------
  108. print u"""#---------------------------------------
  109. #   程序:百度贴吧爬虫
  110. #   版本:0.5
  111. #   作者:why
  112. #   日期:2013-05-16
  113. #   语言:Python 2.7
  114. #   操作:输入网址后自动只看楼主并保存到本地文件
  115. #   功能:将楼主发布的内容打包txt存储到本地。
  116. #---------------------------------------
  117. """

  118. # 以某小说贴吧为例子
  119. # bdurl = 'http://tieba.baidu.com/p/2296712428?see_lz=1&pn=1'

  120. print u'请输入贴吧的地址最后的数字串:'
  121. bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))

  122. #调用
  123. mySpider = Baidu_Spider(bdurl)
  124. mySpider.baidu_tieba()
复制代码







一个自动下载MP3的代码




  1. import urllib,time
  2. import xml.etree.ElementTree as elementtree
  3. url='http://box.zhangmen.baidu.com/x?op=12&count=1&title=songname&rs=counts'
  4. songn=raw_input('请输入歌曲名称').decode('utf8')
  5. songname=urllib.quote(songn.encode('gb2312'))
  6. songurl=url.replace('songname',songname).replace('counts','1')
  7. print songurl
  8. fi=urllib.urlretrieve(songurl)[0]
  9. time.sleep(0.5)
  10. print fi
  11. tree=elementtree.ElementTree(file=fi)
  12. path=tree.getiterator('encode')[0].text
  13. name=tree.getiterator('decode')[0].text
  14. print path,name
  15. path='/'.join(path.split('/')[:-1])
  16. song=path+'/'+name
  17. print song
  18. print 'loading...'
  19. print type(songn),type(song)
  20. urllib.urlretrieve(song,songn+'.'+name.split('.')[-1])
复制代码


只是转载,正确不敢保证,Python高手速来!

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python replace COMPILE counter import 百度贴吧 python import 超链接

已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
kychan + 50 精彩帖子
zbin7451f + 100 + 5 + 5 + 5 对论坛有贡献

总评分: 经验 + 150  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

本帖被以下文库推荐

沙发
jerker 发表于 2015-2-6 23:03:51

回帖奖励 +6

已有 1 人评分论坛币 收起 理由
fantuanxiaot + 2 精彩帖子

总评分: 论坛币 + 2   查看全部评分

藤椅
faruto 发表于 2015-2-6 23:04:38

回帖奖励 +6

转载学习是一方面。

另一方面多发些自己的原创的东西。哪怕简单的尝试。O(∩_∩)O
已有 1 人评分经验 论坛币 收起 理由
fantuanxiaot + 12 + 12 洋哥言之有理,只不过最近主要在看论文,偶.

总评分: 经验 + 12  论坛币 + 12   查看全部评分

板凳
zhukeming 发表于 2015-2-6 23:05:43
初学者学习来了
已有 1 人评分论坛币 收起 理由
fantuanxiaot + 3 精彩帖子

总评分: 论坛币 + 3   查看全部评分

报纸
zhukeming 发表于 2015-2-6 23:06:30
居然没中奖?哈哈
已有 1 人评分论坛币 收起 理由
fantuanxiaot + 2 精彩帖子

总评分: 论坛币 + 2   查看全部评分

地板
zhukeming 发表于 2015-2-6 23:07:24
居然没中?
已有 1 人评分论坛币 收起 理由
fantuanxiaot + 7 精彩帖子

总评分: 论坛币 + 7   查看全部评分

7
fantuanxiaot 发表于 2015-2-6 23:51:27
faruto 发表于 2015-2-6 23:04
转载学习是一方面。

另一方面多发些自己的原创的东西。哪怕简单的尝试。O(∩_∩)O
知道了 洋哥

8
fantuanxiaot 发表于 2015-2-6 23:51:50
zhukeming 发表于 2015-2-6 23:07
居然没中?

9
Crsky7 发表于 2015-2-6 23:55:29

回帖奖励 +6

[转载]基于Python的爬虫代码
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
fantuanxiaot + 5 + 2 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 5  论坛币 + 2  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

10
faruto 发表于 2015-2-7 00:20:44
fantuanxiaot 发表于 2015-2-6 23:51
知道了 洋哥
已有 1 人评分论坛币 收起 理由
fantuanxiaot + 6 精彩帖子

总评分: 论坛币 + 6   查看全部评分

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

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2025-12-31 23:26