Python Code:

from bs4 import BeautifulSoup
import requests
import os
import  sys
import time
path = os.getcwd()
print(path)
print("本工具请配合笔趣阁小说网使用:http://www.xbiquge.la/")
name=input("请输入要保存的书名:")
name='/'+name+".txt"
url=input("请输入开始章节地址:")
endurl=input("请输入结束章节地址:")
passage=1
head1={}
head1['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19'
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4080.0 Safari/537.36 Edg/82.0.453.2'
while 1:
    r=requests.get(url,headers=head)
    while r.status_code==503:
        #print("已触发风控机制,切换伪装头")
        #time.sleep(5)
        r=requests.get(url,headers=head1)
    r.encoding=r.apparent_encoding
    soup=BeautifulSoup(r.text,"html.parser")
    nexturl="http://www.xbiquge.la"+soup.select("#wrapper > div.content_read > div > div.bookname > div.bottem1 > a:nth-child(4)")[0]['href']
    title=soup.select("#wrapper > div.content_read > div > div.bookname > h1")[0].get_text()
    text=soup.select("#content")[0].get_text()
    with open(path+name,'a',encoding='utf-8') as f:
        for x in range(50):
            f.write(" ")
        for y in title:
            f.write(y)
        f.write('\n')
        cnt=1
        for z in text:
            if cnt<len(text)-97:
                f.write(z)
            cnt=cnt+1
        f.write('\n')
    print("已成功爬取%d章"%passage)
    passage=passage+1
    if url==endurl:
        break
    url=nexturl
print("已爬取完所有目标章节!")
os.system("pause")

_OOS3{2{D7GEDFOE39BP%0M.png

Last modification:June 15th, 2020 at 10:51 pm
如果觉得我的文章对你有用,请随意赞赏