python簡單爬取筆趣網(wǎng)小說

  • 查看筆趣網(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)容中的&nbsp替換為空格
            context = con.replace('\xa0',' ')
            print(context)
            context=context+'\n'
            f.writelines(context)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容