笔趣看小说全部章节爬取实战
生活随笔
收集整理的這篇文章主要介紹了
笔趣看小说全部章节爬取实战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import requests
from bs4 import BeautifulSoup
import os
# 本地寫入
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
def getPage(url):re=requests.get(url,headers=headers)re.raise_for_status()re.encoding=re.apparent_encodingsoup=BeautifulSoup(re.text,'lxml')return soup
if __name__ == '__main__':url='https://www.bqkan.com/38_38836/'soup=getPage(url)book_name=soup.select('.info h2')[0].textbook_author=soup.select('.small span')[0].textlast_time=soup.select('.small .last')[0].textlast_chapter=soup.select('.small .last a')[0].textprint("書名:",book_name,book_author,last_time,"最新章節:",last_chapter)# 以書名創建文件夾k=os.path.exists(book_name) # k為bool類型if k==False:# 沒有才創建os.makedirs(book_name)# 獲取所有單章鏈接links=soup.select('.listmain dd a')# 前12個為最新章節,其余為正式章節,包含最新章節for item in links[12:]:href='https://www.bqkan.com'+item['href']print(href,item.string)soup=getPage(href)content=soup.select('.showtxt')[0].textcontent=content.replace('app2();read2(); ','')content=content.replace(' ','\n\n')print(">>>正在寫入文件")with open("%s/%s.txt"%(book_name,item.string),'w',encoding='utf-8') as file:file.write(content)print(">>>全部章節爬取完畢!")
總結
以上是生活随笔為你收集整理的笔趣看小说全部章节爬取实战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gitee操作极速上手指南
- 下一篇: 爬虫Selenium报错“cannot