python爬蟲學習(2)-抓取百度貼吧內容
# -*- coding: utf-8
import urllib2
import urllib
import re,os
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Tiebar:
#初始化資料
def __init__(self,base_url,see_lz):
self.base_url = base_url
self.see_lz = '?see_lz=' + see_lz
self.page = 1
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
self.headers = { 'User-Agent' : self.user_agent }
self.tool = Tool()
self.out_put_file = 'd:/python/test/out.txt'
#獲取頁面內容的方法
def get_cotent(self,page):
try:
url = self.base_url + self.see_lz + '&pn=' + str(page)
request = urllib2.Request(url,headers=self.headers)
response = urllib2.urlopen(request)
act_url = response.geturl()
print 'init url=',url,'act url=',act_url
if url == act_url:
content = response.read()
return content
else:
return None
except urllib2.URLError, e:
if hasattr(e,"reason"):
print u"連線貼吧頁面失敗,錯誤原因",e.reason
return None
#獲取帖子主題
def get_titile(self):
content = self.get_cotent(1)
pattern = re.compile('<h3 .*?>(.*?)</h3>')
result = re.search(pattern,content)
if result:
return result.group(1).strip()
else:
return None
#獲取帖子的頁數
def get_page_num(self):
content = self.get_cotent(1)
pattern = re.compile('<li class="l_reply_num.*?.*?<span.*?>(.*?)',re.S)
result = re.search(pattern,content)
if result:
return result.group(1).strip()
else:
return None
#獲取帖子內容
def get_tiebar(self,page):
content = self.get_cotent(page).decode('utf-8')
str = ''
if not content:
print "抓取完畢"
return None
patt_content = re.compile('<a data-field=.*?class="p_author_name j_user_card".*?>(.*?)</a>.*?'
+ '<div id=".*?" class="d_post_content j_d_post_content "> '
+ '(.*?)
',re.S)import urllib2
import urllib
import re,os
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Tiebar:
#初始化資料
def __init__(self,base_url,see_lz):
self.base_url = base_url
self.see_lz = '?see_lz=' + see_lz
self.page = 1
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
self.headers = { 'User-Agent' : self.user_agent }
self.tool = Tool()
self.out_put_file = 'd:/python/test/out.txt'
#獲取頁面內容的方法
def get_cotent(self,page):
try:
url = self.base_url + self.see_lz + '&pn=' + str(page)
request = urllib2.Request(url,headers=self.headers)
response = urllib2.urlopen(request)
act_url = response.geturl()
print 'init url=',url,'act url=',act_url
if url == act_url:
content = response.read()
return content
else:
return None
except urllib2.URLError, e:
if hasattr(e,"reason"):
print u"連線貼吧頁面失敗,錯誤原因",e.reason
return None
#獲取帖子主題
def get_titile(self):
content = self.get_cotent(1)
pattern = re.compile('<h3 .*?>(.*?)</h3>')
result = re.search(pattern,content)
if result:
return result.group(1).strip()
else:
return None
#獲取帖子的頁數
def get_page_num(self):
content = self.get_cotent(1)
pattern = re.compile('<li class="l_reply_num.*?.*?<span.*?>(.*?)',re.S)
result = re.search(pattern,content)
if result:
return result.group(1).strip()
else:
return None
#獲取帖子內容
def get_tiebar(self,page):
content = self.get_cotent(page).decode('utf-8')
str = ''
if not content:
print "抓取完畢"
return None
patt_content = re.compile('<a data-field=.*?class="p_author_name j_user_card".*?>(.*?)</a>.*?'
+ '<div id=".*?" class="d_post_content j_d_post_content "> '
+ '(.*?)
相關文章
- Python爬蟲實戰(2):百度貼吧帖子Python爬蟲
- python爬蟲-抓取內涵吧內涵段子Python爬蟲
- 網路爬蟲——爬百度貼吧爬蟲
- Python爬蟲,抓取淘寶商品評論內容!Python爬蟲
- 學習python做爬蟲主要學習哪些內容呢?Python爬蟲
- 三種 Python 網路內容抓取工具與爬蟲Python爬蟲
- 使用 Python 編寫多執行緒爬蟲抓取百度貼吧郵箱與手機號Python執行緒爬蟲
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- Python爬蟲學習(2): httplibPython爬蟲HTTP
- python爬蟲—學習筆記-2Python爬蟲筆記
- python爬蟲學習(1)-抓取糗事百科笑話Python爬蟲
- ScienceDirect內容爬蟲爬蟲
- Python爬蟲抓取股票資訊Python爬蟲
- 爬取百度貼吧實戰,python教你如何獲取Python
- Python爬去貼吧圖片Python
- python爬蟲學習(3)-抓取廖雪峰python教程並製作成PDF文件Python爬蟲
- python爬蟲學習1Python爬蟲
- python爬蟲:使用BeautifulSoup修改網頁內容Python爬蟲網頁
- 爬蟲,可用於增加訪問量和抓取網站全頁內容爬蟲網站
- JB的Python之旅-爬蟲篇-新浪微博內容爬取Python爬蟲
- PHP 爬蟲爬取社群文章內容PHP爬蟲
- python爬蟲抓取哈爾濱天氣資訊(靜態爬蟲)Python爬蟲
- Python爬蟲學習筆記-2.Requests庫Python爬蟲筆記
- python爬蟲是什麼?學習python爬蟲難嗎Python爬蟲
- 用Python爬蟲抓取代理IPPython爬蟲
- Python 爬蟲網頁內容提取工具xpath(一)Python爬蟲網頁
- Python 爬蟲網頁內容提取工具xpath(二)Python爬蟲網頁
- Python爬蟲學習系列教程Python爬蟲
- Python爬蟲爬取B站up主所有動態內容Python爬蟲
- scrapy定製爬蟲-爬取javascript內容爬蟲JavaScript
- python爬蟲學習(4)抓取鏈家網二手房資料Python爬蟲
- 什麼是爬蟲?學習Python爬蟲難不難?爬蟲Python
- scrapy爬蟲成長日記之將抓取內容寫入mysql資料庫爬蟲MySql資料庫
- python爬蟲之快速對js內容進行破解Python爬蟲JS
- Python爬蟲抓取技術的門道Python爬蟲
- python爬蟲之抓取小說(逆天邪神)Python爬蟲
- Python爬蟲使用代理proxy抓取網頁Python爬蟲網頁
- python爬蟲-抓取騰訊招聘資訊頁面Python爬蟲