00 00 00:00:00 周一
提交收录

Python爬虫基础知识是什么

更新时间:2019-03-17 13:51:00 内容编辑:海静 所属分类:建站经验

资源介绍

项目内容:
用Python写的百度贴吧的网络爬虫。
运用方法:
新建一个BugBaidu.py文件,然后将代码复制到里边后,双击运转。

程序功用:
将贴吧中楼主发布的内容打包txt存储到本地。
原理解说:
首要,先阅读一下某一条贴吧,点击只看楼主并点击第二页之后url发生了一点改变,变成了:
http://tieba.baidu.com/p/2296712428?see_lz=1&pn=1
能够看出来,see_lz=1是只看楼主,pn=1是对应的页码,记住这一点为今后的编写做准备。
这就是咱们需求运用的url。
接下来就是检查页面源码。
首要把标题抠出来存储文件的时分会用到。
能够看到百度运用gbk编码,标题运用h1符号:
相同,正文部分用div和class归纳符号,接下来要做的仅仅用正则表达式来匹配即可。
运转截图:

生成的txt文件:

以下为本次项目源码:
# -*- coding: utf-8 -*-
#---------------------------------------
# 程序:百度贴吧爬虫
# 版别:0.5
# 作者:why
# 日期:2013-05-16
# 言语:Python 2.7
# 操作:输入网址后主动只看楼主并保存到本地文件
# 功用:将楼主发布的内容打包txt存储到本地。
import string
import urllib2
import re
#----------- 处理页面上的各种标签 -----------
class HTML_Tool:
# 用非 贪婪形式 匹配 \\t 或许 \\n 或许 空格 或许 超链接 或许 图片
BgnCharToNoneRex = re.compile( \"(\\t|\\n| ||)\" )
# 用非 贪婪形式 匹配 恣意<>标签
EndCharToNoneRex = re.compile( \"<.*?>\" )
# 用非 贪婪形式 匹配 恣意
标签
BgnPartRex = re.compile( \"\" )
CharToNewLineRex = re.compile( \"(|
||
|
)\")
CharToNextTabRex = re.compile( \"\" )
# 将一些html的符号实体转变为原始符号
replaceTab = [( \"<\"< span=\"\"> , \"<\"< span=\"\"> ),( \">\" , \">\" ),( \"&\" , \"&\" ),( \"&\" , \"\\\"\" ),( \" \" , \" \" )]
def Replace_Char( self ,x):
x = self .BgnCharToNoneRex.sub(\"\",x)
x = self .BgnPartRex.sub( \"\\n \" ,x)
x = self .CharToNewLineRex.sub( \"\\n\" ,x)
x = self .CharToNextTabRex.sub( \"\\t\" ,x)
x = self .EndCharToNoneRex.sub(\"\",x)
for t in self .replaceTab:
x = x.replace(t[ 0 ],t[ 1 ])
return x
class Baidu_Spider:
# 声明相关的特点
def __init__( self ,url):
self .myUrl = url + '?see_lz=1'
self .datas = []
self .myTool = HTML_Tool()
print u '现已发动百度贴吧爬虫,咔嚓咔嚓'
# 初始化加载页面并将其转码贮存
def baidu_tieba( self ):
# 读取页面的原始信息并将其从gbk转码
myPage = urllib2.urlopen( self .myUrl).read().decode( \"gbk\" )
# 核算楼主发布内容一共有多少页
endPage = self .page_counter(myPage)
# 获取该帖的标题
title = self .find_title(myPage)
print u '文章称号:' + title
# 获取终究的数据
self .save_data( self .myUrl,title,endPage)
#用来核算一共有多少页
def page_counter( self ,myPage):
# 匹配 \"共有12页\" 来获取一共有多少页
myMatch = re.search(r 'class=\"red\">(\\d+?)', myPage, re.S)
if myMatch:
endPage = int(myMatch.group( 1 ))
print u '爬虫陈述:发现楼主共有%d页的原创内容' % endPage
else :
endPage = 0
print u '爬虫陈述:无法核算楼主发布内容有多少页!'
return endPage
# 用来寻觅该帖的标题
def find_title( self ,myPage):
# 匹配 xxxxxxxxxx 找出标题
myMatch = re.search(r '(.*?)' , myPage, re.S)
title = u '暂无标题'
title = myMatch.group( 1 )
print u '爬虫陈述:无法加载文章标题!'
# 文件名不能包括以下字符: \\ / : * ? \" <> |
title = title.replace( '\\\\',' ').replace(' / ',' ').replace(' : ',' ').replace(' * ',' ').replace(' ? ',' ').replace(' \" ',' ').replace(' > ',' ').replace(' < ',' ').replace(' | ',' ')
return title
# 用来存储楼主发布的内容
def save_data( self ,url,title,endPage):
# 加载页面数据到数组中
self .get_data(url,endPage)
# 翻开本地文件
f = open(title+ '.txt' , 'w+' )
f.writelines( self .datas)
f.close()
print u '爬虫陈述:文件已下载到本地并打包成txt文件'
print u '请按恣意键退出...'
raw_input();
# 获取页面源码并将其存储到数组中
def get_data( self ,url,endPage):
url = url + '&pn='
for i in range( 1 ,endPage+ 1 ):
print u '爬虫陈述:爬虫%d号正在加载中...' % i
myPage = urllib2.urlopen(url + str(i)).read()
# 将myPage中的html代码处理并存储到datas里边
self .deal_data(myPage.decode( 'gbk' ))
# 将内容从页面代码中抠出来
def deal_data( self ,myPage):
myItems = re.findall( 'id=\"post_content.*?>(.*?)
',myPage,re.S)
for item in myItems:
data = self .myTool.Replace_Char(item.replace( \"\\n\" ,\"\").encode( 'gbk' ))
self .datas.append(data+ '\\n' )
#-------- 程序入口处 ------------------
print u \"\"\"#---------------------------------------
# 程序:百度贴吧爬虫
# 版别:0.5
# 作者:why
# 日期:2013-05-16
# 言语:Python 2.7
# 操作:输入网址后主动只看楼主并保存到本地文件
# 功用:将楼主发布的内容打包txt存储到本地。
#---------------------------------------
\"\"\"

转载请注明出处。