前言:
小說(shuō)作為在自己空閑時(shí)間下的消遣工具,對(duì)我們打發(fā)空閑時(shí)間很有幫助,而我們?cè)诰W(wǎng)站上面瀏覽小說(shuō)時(shí)會(huì)被廣告和其他一些東西影響我們的觀看體驗(yàn),而這時(shí)我們就可以利用爬蟲(chóng)將我們想要觀看的小說(shuō)下載下來(lái),這樣就不會(huì)擔(dān)心廣告的影響了。
一:環(huán)境配置
Python版本:3.7.3
IDE:PyCharm
所需庫(kù):requests,lxml,time
二:準(zhǔn)備工作
1:安裝好我們所需要的庫(kù)。?
2:我們需要在電腦上的指定位置來(lái)創(chuàng)建一個(gè)文件夾來(lái)保存我們爬取的小說(shuō)。
3:需要去下載XPATH插件以便于我們獲取小說(shuō)的名字(資源已上傳,可自行下載安裝)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-697794.html
三:具體代碼實(shí)現(xiàn)
import requests
from lxml import etree
import time
url = 'https://www.biquge365.net/newbook/33411/'
head = {
'Referer': 'https://www.biquge365.net/book/33411/',
'users-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.39'
}
response = requests.get(url,headers = head,verify = False)
# print(response.text)
html = etree.HTML(response.text)
novel_name = html.xpath('/html/body/div[1]/div[3]/div[1]/h1/text()')[0]
novel_directory = html.xpath('/html/body/div[1]/div[4]/ul/li[*]/a/@href')
#由于網(wǎng)站可能具有反扒措施,所以我們?cè)O(shè)置一下時(shí)間,防止被反扒
time.sleep(6)
for i in novel_directory:
com_url = 'https://www.biquge365.net'+i
response2 = requests.get(com_url,headers=head)
html2 = etree.HTML(response2.text)
novel_chapter = html2.xpath('//*[@id="neirong"]/h1/text()')[0]
novel_content = '\n'.join(html2.xpath('//*[@id="txt"]/text()'))
with open('E:\\python源碼\\爬蟲(chóng)教程\\小說(shuō).txt'+novel_chapter+'.txt','w',encoding='utf-8') as file:
file.write(novel_chapter+'\n'+novel_content+'\n')
file.close()
print("下載成功"+novel_chapter)
四:結(jié)果展示
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-697794.html
到了這里,關(guān)于爬蟲(chóng)源碼---爬取自己想要看的小說(shuō)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!