bsp;
def switch_arctiles_to_list(self, articles):
' 把articles轉換成數據字典 '
articles_list = []
i = 1
if articles:
for article in articles.items():
self.log(u'開始整合(%d/%d)' % (i, len(articles)))
articles_list.append(self.parse_one_article(article))
i += 1
# break
return articles_list
def parse_one_article(self, article):
' 解析單篇文章 '
article_dict = { }
article = article('.weui_media_box[id]')
title = article('h4[class="weui_media_title"]').text()
self.log('標題是: %s' % title)
url = '買粉絲://mp.weixin.qq.買粉絲' + article('h4[class="weui_media_title"]').attr('hrefs')
self.log('地址為: %s' % url)
summary = article('.weui_media_desc').text()
self.log('文章簡述: %s' % summary)
date = article('.weui_media_extra_info').text()
self.log('發表時間為: %s' % date)
pic = self.parse_買粉絲ver_pic(article)
買粉絲ntent = self.parse_買粉絲ntent_by_url(url).買粉絲()
買粉絲ntentfiletitle=self.kw+'/'+title+'_'+date+'.買粉絲'
self.save_買粉絲ntent_file(買粉絲ntentfiletitle,買粉絲ntent)
return {
'title': title,
'url': url,
'summary': summary,
'date': date,
'pic': pic,
'買粉絲ntent': 買粉絲ntent
}
def parse_買粉絲ver_pic(self, article):
' 解析文章封面圖片 '
pic = article('.weui_media_hd').attr('style')
p = re.買粉絲pile(r'background-image:url(.∗?)')
rs = p.findall(pic)
self.log( '封面圖片是:%s ' % rs[0] if len(rs) > 0 else '')
return rs[0] if len(rs) > 0 else ''
def parse_買粉絲ntent_by_url(self, url):
' 獲取文章詳情內容 '
page_買粉絲 = self.get_selenium_js_買粉絲(url)
return pq(page_買粉絲)('#js_買粉絲ntent')
def save_買粉絲ntent_file(self,title,買粉絲ntent):
' 頁面內容寫入文件 '
with open(title, 'w') as f:
f.write(買粉絲ntent)
def save_file(self, 買粉絲ntent):
' 數據寫入文件 '
with open(self.kw+'/'+self.kw+'.txt', 'w') as f:
f.write(買粉絲ntent)
def log(self, msg):
' 自定義log函數 '
print u'%s: %s' % (time.strftime('%Y-%m-%d %H:%M:%S'), msg)
def need_verify(self, selenium_買粉絲):
' 有時候對方會封鎖ip,這里做一下判斷,檢測買粉絲中是否包含id=verify_change的標簽,有的話,代表被重定向了,提醒過一陣子重試 '
return pq(selenium_買粉絲)('#verify_change').text() != ''
def create_dir(self):
'創建文件夾'
if not os.path.exists(self.kw):
os.makedirs(self.kw)
def run(self):
' 爬蟲入口函數 '
#Step 0 : 創建買粉絲命名的文件夾
self.create_dir()
# Step 1:GET請求到搜狗買粉絲引擎,以買粉絲買粉絲英文名稱作為查詢關鍵字
self.log(u'開始獲取,買粉絲買粉絲英文名為:%s' % self.kw)
self.log(u'開始調用sougou搜索引擎')
sougou_search_買粉絲 = self.get_search_result_by_kw()
# Step 2:從搜索結果頁中解析出買粉絲主頁鏈接
self.log(u'獲取sougou_search_買粉絲成功,開始抓取買粉絲對應的主頁wx_url')
wx_url = self.get_wx_
2024-07-24 09:26
2024-07-24 09:17
2024-07-24 09:09
2024-07-24 09:08
2024-07-24 08:17
2024-07-24 07:47