from urllib.request import quote, unquote
import requests
from bs4 import BeautifulSoup
import sys
import time
content = input('請輸入你想要查找的小說名:')
initial_content = content
keyword = quote(initial_content,encoding='gb2312')#筆趣閣的是gb2312編碼
url = 'http://www.biquge.com.tw/modules/article/soshu.php?searchkey='+keyword
print(url)
re = requests.get(url) #筆趣閣搜索url
retype=re.apparent_encoding
re.encoding = retype
print(re.status_code)
html = re.text
soup = BeautifulSoup(html, 'html.parser')
fileName = '/Users/john/Desktop/小說/'+initial_content+'.txt'
print(fileName)
file = open(fileName, 'a', encoding='utf-8')
chapters = soup.find_all(id='list')
info = soup.find_all(id='info')
for link in info:
file.write(link.get_text())#書籍作者信息
download_soup = BeautifulSoup(str(chapters), 'html.parser')
arr = []
for child in download_soup.dl.children: #dl下所有子節(jié)點(diǎn)
if hasattr(child, 'href') and child.a != None:
arr.append(child.get_text())
numbel = len(arr)
print(numbel)
index= 1
time1 = time.time ()#獲取當(dāng)前時(shí)間(秒)
for child in download_soup.dl.children: #dl下所有子節(jié)點(diǎn)
if hasattr(child, 'href') and child.a != None:
file.write(child.get_text() + '\n' + '-----------------------------------------------' + '\n')
url = 'http://www.biquge.com.tw/' + child.a['href']
# print(url)
reponse_dl = requests.get(url)
type_dl = reponse_dl.apparent_encoding
reponse_dl.encoding = type_dl
html_dl = reponse_dl.text
soup_dl = BeautifulSoup(html_dl, 'html.parser')
contents = soup_dl.find_all(id='content') #帶著<div id="content">
# soup_text = BeautifulSoup(str(contents), 'lxml')
# print(soup_text)
for link in contents:
#print(link.get_text())
file.write(link.get_text() + '\n\n')
print("已下載:%.3f%%" % float(index / numbel*100))#爬取進(jìn)度
index += 1
time2 = time.time()
tt = (time2 - time1)
print('花費(fèi)時(shí)間:' + str(tt) + '秒')
file.close()
主要是根據(jù)筆趣閣的搜索url,加上書名,拿到目錄,然后一章章下載到本地,我是mac,window需要改下保存地址
python爬蟲-筆趣閣
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 聲明:本文講解的實(shí)戰(zhàn)內(nèi)容,均僅用于學(xué)習(xí)交流,請勿用于任何商業(yè)用途! 一、前言 強(qiáng)烈建議:請?jiān)陔娔X的陪同下,閱讀本文...
- 關(guān)于bs4,官方文檔的介紹已經(jīng)非常詳細(xì)了,傳送:Beautifulsoup 4官方文檔,這里我把它組織成自己已經(jīng)消...
- Date:2016-9-21update:2016-9-30By:Black Crow 前言: 終于進(jìn)入到網(wǎng)絡(luò)頁面...
- Python爬蟲入門(urllib+Beautifulsoup) 本文包括:1、爬蟲簡單介紹2、爬蟲架構(gòu)三大模塊3...
- 一、在配置文件中,存放oracle連接字符串 1.連接字符串: "data source=127.0.0.1/TE...