日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python写的一个贴吧爬虫_Python-简单百度贴吧爬虫

發布時間:2024/4/18 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python写的一个贴吧爬虫_Python-简单百度贴吧爬虫 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里以上海工會管理職業學院為例子,目標是獲取以下內容。

通過觀察網頁編碼,可以發現以下結構:

標題:

發貼人:

內容:

今天廣播員是誰吶歌單和我好像吶求勾搭【另外,別讓那個主播Jason出現了,難受】

接下來就是通過urllib2+re(正則表達式)來進行網頁處理。

說明

1.在處理網頁的時候,如果貼子是置頂的話,有時候會缺失內容項,所以添加了處理置頂貼的函數。

2.網頁會有重復貼子的情況,增加了去重功能

如果如下,可以看到大部分的都是些無用的東西;沒有太

#__author__='ZHENGT'

# -*- coding: utf-8 -*-

#爬取百度貼吧的內容

#參考資料:

#http://www.imekaku.com/2015/08/31/python-work-tieba2/

#http://blog.csdn.net/u010412719/article/details/50199047

#http://python.jobbole.com/81359/

#備注:百度貼吧不同頁下載下來的數據會有很多重復項,需要進行去重處理

import urllib2

import re

import os

#上海工會管理職業學院

GH_url='http://tieba.baidu.com/f?ie=utf-8&kw=' \

'%E4%B8%8A%E6%B5%B7%E5%B7%A5%E4%BC%9A%E7%' \

'AE%A1%E7%90%86%E8%81%8C%E4%B8%9A%E5%AD%A6%E9%99%A2&pn='

#上海商學院

SXY_url='http://tieba.baidu.com/f?kw=' \

'%E4%B8%8A%E6%B5%B7%E5%95%86%E5%AD%A6%E9%99%A2&ie=utf-8&pn='

#上海應用技術大學

YJD_url='http://tieba.baidu.com/f?kw=' \

'%E4%B8%8A%E6%B5%B7%E5%BA%94%E7%94%A8%E6%8A%80%E6%9C%AF%E5%AD%A6%E9%99%A2&ie=utf-8&pn=1'

f1="YJD.txt"

ID_list={} #用于記錄貼子ID,去重

#定義百度函數

def Baidu_Tieba(url,i_page):

try:

print '正在下載'+str(i_page)+'頁..........' #獲取抓取網頁

html_Tieba=urllib2.urlopen(url+str(i_page)).read()

print type(html_Tieba) #顯示類別,一般為str

return html_Tieba #返回值

except Exception,e: #Error

print e

def PraserHTML(WebData,i_page): #處理網頁信息,WebData->>String

TopNum=0 #初始化

Titles=GetTitle(WebData) #Type-->>List

Posters=GetPoster(WebData) #Type-->>List

Contents=GetContent(WebData) #Type-->>List

IconTop=GetTop(WebData) #Type-->List

TopNum=len(IconTop) #置頂貼子數

if TopNum>0: #記錄頁面置頁貼子

for i in range(0,len(Titles)): #根據標題列表循環

title=ReplaceTitle(Titles[i])

IDs = title.split(' ') #按空格分割,獲取ID號碼

ID_list[IDs[0]] = IDs[1] #添加字典

poster=ReplacePoster(Posters[i])

if i<=TopNum: #處理置頂貼

content = "IconTop"

else:

content=ReplaceContent(Contents[i-TopNum])

Write_To_File(title,poster,content)

else:

for i in range(0,len(Titles)): #根據標題列表循環

title=ReplaceTitle(Titles[i])

IDs=title.split(' ') #按空格分割,獲取ID號碼

if not ID_list.has_key(IDs[0]):

ID_list[IDs[0]]=IDs[1] #添加字典

poster=ReplacePoster(Posters[i])

content=ReplaceContent(Contents[i])

Write_To_File(title,poster,content)

def ReplaceTitle(item): #替換Title不需要字符

removeHead=re.compile('

else:

return "NA"

def Write_To_File(title,poster,content): #數據寫入txt文件

f=open(f1,'a')

f.write(title+"--->>>"+poster+"--->>>"+content) #主題,發貼人,內容

f.write("\n")

f.close()

def GetTitle(Data): #獲取當前頁主題

pattern=re.compile(r'

data=re.findall(pattern,Data)

return data

def GetTop(Data): #獲取當前頁內容,獲取置頂貼子

pattern=re.compile(r'')

data=re.findall(pattern,Data)

return data

if __name__=='__main__': #

if os.path.exists(f1): os.remove(f1) #初始化文件

for i in range(1,11): #指定需要循環的頁數

html_data=Baidu_Tieba(YJD_url,i)

PraserHTML(html_data,i)

# Write_To_File("Page----",str(i),"-----Done")

print "Done-----------------"

exit(0)

總結

以上是生活随笔為你收集整理的python写的一个贴吧爬虫_Python-简单百度贴吧爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。