求帮忙盗qq号百度贴吧吧个人用户隐藏的问题~~帮忙get~~

1944人阅读
python(25)
《python爬虫实战》:爬取贴吧上的帖子
经过前面两篇例子的练习,自己也对爬虫有了一定的经验。
由于目前还没有利用BeautifulSoup库,因此关于爬虫的难点还是正则表达式的书写。
但是,当写几个正则表达式之后,发现还是有一定的规则可循的,只要我们的目的明确,想得到网页的什么,我们就直接在该网页上,找到我们想要的内容在html代码中出现的格式是怎么样的。
例如,我们想获取贴吧上帖子的标题。
解决方法为:在相应的网页的html代码中找到title出现的地方。然后提取正则表达式。
根据上面的截图:提取的正则表达式如下:
pile(r'&h3 class="core_title_txt pull-left text-overflow .*?&(.*?)&/h3&',re.S)
因此,得到帖子的标题的代码可以这么写。
def getPageTitle(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&h3 class="core_title_txt pull-left text-overflow .*?&(.*?)&/h3&',re.S)
title=re.search(pattern,content)
return title.group(1).strip()
print None
同样的道理,我们可以得到获取帖子作者的代码如下:
def getPageAuthor(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&div class="louzhubiaoshi
j_louzhubiaoshi" author="(.*?)"&',re.S)
author=re.search(pattern,content)
if author:
print author.group(1).strip()
return author.group(1).strip()
print None
同样的道理,可以得到任何我们想要的内容。比例:帖子的回复总数等。
下面是完整代码:
import urllib2
import urllib
class Tool:
pile('&img.*?&| {7}|')
pile('&a.*?&|&/a&')
pile('&tr&|&div&|&/div&|&/p&')
pile('&td&')
pile('&p.*?&')
pile('&br&&br&|&br&')
pile('&.*?&')
def replace(self,x):
x=re.sub(self.removeImg,"",x)
x=re.sub(self.removeAddr,"",x)
x=re.sub(self.replaceLine,"\n",x)
x=re.sub(self.replaceTD,"\t",x)
x=re.sub(self.replacePara,"\n
x=re.sub(self.replaceBR,"\n",x)
x=re.sub(self.removeExtraTag,"",x)
return x.strip()
class BaiduTieBa:
def __init__(self,url,seeLZ,floorTag):
self.url=url
self.seeLZ="?see_lz="+str(seeLZ)
self.tool=Tool()
self.file=None
self.defultTitle="百度贴吧"
self.floorTag=floorTag
self.floor=1
def getPageContent(self,pageNum):
url=self.url+self.seeLZ+"&pn="+str(pageNum)
user_agent="Mozilla/5.0 (Windows NT 6.1)"
headers={"User-Agent":user_agent}
request=urllib2.Request(url,headers=headers)
response=urllib2.urlopen(request)
content=response.read().decode("utf-8")
return content
except urllib2.URLError,e:
if hasattr(e,"reason"):
print e.reason
def getPageTitle(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&h3 class="core_title_txt pull-left text-overflow .*?&(.*?)&/h3&',re.S)
title=re.search(pattern,content)
print title.group(1).strip()
return title.group(1).strip()
print None
def getPageAuthor(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&div class="louzhubiaoshi
j_louzhubiaoshi" author="(.*?)"&',re.S)
author=re.search(pattern,content)
if author:
print author.group(1).strip()
return author.group(1).strip()
print None
def getPageTotalPageNum(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&li class="l_reply_num".*? style="margin-right:3px"&(.*?)&/span&.*?&span class="red"&(.*?)&/span&',re.S)
totalPageNum=re.search(pattern,content)
if totalPageNum:
print totalPageNum.group(1).strip(),totalPageNum.group(2).strip()
return totalPageNum.group(1).strip(),totalPageNum.group(2).strip()
print "没找到"
print None
def getContent(self,pageNum):
content=self.getPageContent(pageNum)
pile(r'&div id="post_content_.*?&(.*?)&/div&',re.S)
items=re.findall(pattern,content)
contents=[]
for item in items:
tempContent="\n"+self.tool.replace(item)+"\n"
contents.append(tempContent.encode("utf-8"))
return contents
def writedata2File(self,contents):
for item in contents:
if self.floorTag=="1":
floorLine=u"\n"+str(self.floor)+u"-------------------\n"
self.file.write(floorLine)
print u"正在往文件中写入第"+str(self.floor)+u"楼的内容"
self.file.write(item)
self.floor+=1
def newFileAccTitle(self,title):
if title is not None:
self.file=open(title+".txt","w+")
self.file=open(defultTitle+".txt","w+")
def start(self,pageNum):
content=self.getPageContent(pageNum)
title=self.getPageTitle(pageNum)
self.newFileAccTitle(title)
author=self.getPageAuthor(pageNum)
contents=self.getContent(pageNum)
self.writedata2File(contents)
except IOError,e:
print "写入文件发生异常,原因"+e.message
print "写入文件完成!"
url="http://www./p/"
seeLZ=input("input see_lz:")
pageNum=input("input pageNum:")
floorTag=raw_input("input floorTag:")
baidutieba=BaiduTieBa(url,seeLZ,floorTag)
baidutieba.start(pageNum)
获取代码,可以点击这里:
参考地址:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:175419次
积分:7278
积分:7278
排名:第2250名
原创:539篇
转载:10篇
评论:52条
文章:12篇
阅读:16717
文章:11篇
阅读:4573
文章:16篇
阅读:3147
阅读:4941
阅读:3993
文章:22篇
阅读:13570
(3)(14)(4)(31)(47)(24)(26)(50)(57)(23)(8)(18)(77)(66)(24)(16)(33)(24)(5)404 Not Found
The requested URL /2719.html was not found on this server.

我要回帖

更多关于 求帮忙盗qq号百度贴吧 的文章

 

随机推荐