- 查看筆趣網(wǎng)的html結(jié)構(gòu)
-
取小說內(nèi)容
小說章節(jié)內(nèi)容顯示
可以看到該網(wǎng)頁是把小說內(nèi)容放在一個ID為content的div里面的
所以我們可以用.//div[@id='conten']/text()來取到內(nèi)容 -
取小說章節(jié)標題
章節(jié)標題顯示
用google瀏覽器的檢查可以看到章節(jié)標題的位置,可以用.//div[@class="bookname"]/h1/text()來取
-
獲取下一頁鏈接
上下章節(jié)顯示
通過查看,我們可以用.//div[@class="bottem2"]/a[contains(text(),"下一章")]/@href來取到下一章鏈接
同時由于我現(xiàn)在是第一章,可以看到上一章顯示并不是上一章節(jié),并且沒有帶html結(jié)尾,所以我們可以猜測如果到了最后一個章節(jié)也會顯示和第一章的上一章相同,所以我們可以判斷下一頁的鏈接是否有.html結(jié)尾 - 最后的代碼
-
from lxml import etree
import requests
# header
headers = {
'User-Agent':'Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\
AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36'
}
# 書籍鏈接
base_url = "http://www.biquku.la/8/8797/"
# 最開始的頁面
next_page = "5353807.html"
# 小說存放位置
file = '/Users/zz/Documents/resin/8798.txt'
with open(file, 'w+') as f:
while ".html" in next_page:
url = base_url + next_page
response = requests.get(url,headers = headers)
response.encoding = 'utf8'
html = response.text
html_str = etree.HTML(html)
# 從html找到下一章的鏈接
next_page = html_str.xpath('.//div[@class="bottem2"]/a[contains(text(),"下一章")]/@href')
print(next_page[0])
next_page = next_page[0]
# 取到章節(jié)的標題
title = html_str.xpath('.//div[@class="bookname"]/h1/text()')
# 寫入章節(jié)標題,加入換行
title = title[0]+'\n'
f.writelines(title)
print(title)
# 取到章節(jié)內(nèi)容
content = html_str.xpath('.//div[@id="content"]/text()')
for con in content:
# 將取到的章節(jié)內(nèi)容中的 替換為空格
context = con.replace('\xa0',' ')
print(context)
context=context+'\n'
f.writelines(context)


