
image
一、分析頁(yè)面
首先,用命令行工具打開(kāi)mitmweb,如圖:
mitmweb

image
然后,將手動(dòng)配置手機(jī)的代理服務(wù)器為PC機(jī)。
如何配置可以參考之前的文章https://www.cnblogs.com/xyztank/articles/12362470.html
接著,在手機(jī)打開(kāi)想要爬取的軟件“得到App”。

image
根據(jù)代理截獲的數(shù)據(jù),然后進(jìn)行分析,最終定位文章地址列表:
https://entree.igetget.com/bauhinia/v1/class/purchase/article_list
然后,分析服務(wù)器返回的json數(shù)據(jù),可以看到文章的標(biāo)題及地址。

image
二、代碼實(shí)現(xiàn)
from mitmproxy import ctx
import json
from lxml import etree
from selenium import webdriver
def response(flow):
"""
利用mitmdump ui分析出頁(yè)面url
"""
start_url = "https://entree.igetget.com/bauhinia/v1/class/purchase/article_list"
if flow.request.url.startswith(start_url):
text = flow.response.text
data = json.loads(text)
talks = data.get('c').get('article_list')
for talk in talks:
title = talk.get('share_title')
url = talk.get('share_url')
ctx.log.info(str(title))
parse_page(url)
def parse_page(url):
"""
發(fā)現(xiàn)獲得的url頁(yè)面無(wú)法直接解析,
進(jìn)一步分析得出,頁(yè)面信息是通過(guò)js渲染,
但是瀏覽器又能正常顯示頁(yè)面,于是采用selenium方式爬取信息
"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
#通過(guò)headless設(shè)置,讓瀏覽器不再顯示
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)
resouce = driver.page_source
html = etree.HTML(resouce)
title = html.xpath('//h1[@class="title"]/text()')[0]
time = html.xpath('//span[@class="time"]/text()')[0]
content = html.xpath('//div[@class="text"]//p//text()')
content = "".join(content)
print(title, time)
save(title, time, content)
def save(title,time,content):
"""
保存至文本文件中
"""
with open('dedao.txt','a',encoding='utf-8') as fp:
fp.write('\n'.join([title,content,time]))
fp.write('\n' + '='*50 + '\n')
windows平臺(tái)命令行運(yùn)行腳本:
mitmdump -s test_dedao.py

image
這里可以看到控制臺(tái)正在不斷的打印正在爬取的文章標(biāo)題。
三、結(jié)果展示
當(dāng)然,數(shù)據(jù)還可以保存在數(shù)據(jù)庫(kù)中,這里偷個(gè)懶,就直接保存在txt文件中。

image