????最近正好在看這本小說,網上廣告屬實多了點杆勇,而且好多存在斷章的情況贪壳,所以自己去網上下載下來電腦或者手機上看最實際了
1. 首頁
????對沒錯,就是這本都市小說《道德天書》蚜退,從首頁還是很容易能夠獲取到章節(jié)鏈接的
image.png
2. 內容頁
????內容頁面有點小陷阱闰靴,雖然看著簡單,但是實際將頁面的內容打印出來是殘缺的钻注,他只構建了部分文本內容蚂且,實際的內容是需要自己抓包獲取,不信的話可以打印頁面內容看看,是不完整的
image.png
3. 抓包
這里規(guī)則還是比較簡單的幅恋,很容易就找到了對應的數(shù)據包
image.png
我們選擇數(shù)據包的
headers
進入里面顯示的真實鏈接就可以看到具體的內容了
image.png
4. 數(shù)據獲取
???? 所有的數(shù)據來源我們都知道了杏死,就著手開始建設了,首先從首頁遍歷所有章節(jié)的頁面鏈接捆交,從每個章節(jié)的頁面中獲取到標題和內容淑翼,沒錯,這里的內容需要去數(shù)據包中獲取零渐,仔細觀察會發(fā)現(xiàn)數(shù)據包的鏈接恰巧就是網頁主頁鏈接+章節(jié)鏈接的后兩項窒舟,所以能夠很輕易的組合出來,后面的內容無非是獲取诵盼,寫入惠豺。
- 這里因為內容中還是存在廣告银还,所以用
replace
將它剔除了,其他的就是xpath
解析洁墙,寫入了
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@author: maya
@software: Pycharm
@file: tqdm.py
@time: 2019/8/20 14:08
@desc:
'''
import requests
from lxml import etree
headers = {
'cookie': 'Hm_lvt_33b927fed41089db72f5d741701b24f2=1566285504; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; Hm_lpvt_33b927fed41089db72f5d741701b24f2=1566285551',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
'upgrade-insecure-request': '1',
'referer': 'https://www.rzlib.net/b/73/73530/',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
}
def get_html(url):
return requests.get(url, headers=headers)\
.text.replace('如果您覺得《周睿道德天書》還不錯的話蛹疯,請粘貼以下網址分享給你的QQ、'
'微信或微博好友热监,謝謝支持捺弦!', '').replace('( 本書網址:https://www.'
'rzlib.net/b/73/73530/ )', '')
def get_data(url):
html = etree.HTML(get_html(url))
title = html.xpath('//h1/text()')[0].replace('.', '_')
content_url = get_url(url)
content_html = etree.HTML(get_html(content_url)).xpath('//body//text()')
content = ["".join(data.split()) for data in content_html]
return title, content
def get_url(url):
return "https://www.rzlib.net/b/txtt5552/" + url.split('/')[-2] + "/" + url.split('/')[-1]
def write_data(url):
title, content = get_data(url)
with open('books/' + title + '.txt', 'w', encoding='utf-8') as f:
f.write(title.replace('_', '. ') + '\n')
for data in content:
if data != "":
f.write(' ' + data + '\n')
with open('books/books.txt', 'a', encoding='utf-8') as p:
p.write(title.replace('_', '. ') + '\n')
for data in content:
if data != "":
p.write(' ' + data + '\n')
p.write('\n')
def get_total(index_utl):
html = etree.HTML(get_html(index_utl))
urls = html.xpath('//div[@class="ListChapter"][2]/ul/li/a/@href')
for url in urls:
write_data("https://www.rzlib.net" + url)
print("第{}章已完成寫入".format(urls.index(url) + 1))
if __name__ == '__main__':
get_total("https://www.rzlib.net/b/73/73530/")
- 代碼參考Github