柚子快報(bào)激活碼778899分享:第四個(gè)爬蟲(chóng)實(shí)戰(zhàn)代碼
柚子快報(bào)激活碼778899分享:第四個(gè)爬蟲(chóng)實(shí)戰(zhàn)代碼
1.將數(shù)據(jù)存儲(chǔ)到Excel文件中 使用python的第三方庫(kù)xlwt,可以將數(shù)據(jù)寫(xiě)入到Excel種, 我是使用pycharm安裝的xlwt庫(kù)的。 代碼:
import xlwt
# 通過(guò)Workbook()方法創(chuàng)建一個(gè)工作簿
book = xlwt.Workbook(encoding='UTF-8')
# 創(chuàng)建一個(gè)名字為Sheet1的工作表
sheet = book.add_sheet('Sheet1')
# 寫(xiě)入數(shù)據(jù),可以看出第一個(gè)和第二個(gè)參數(shù)為Excel表格的單元格位置,第三個(gè)寫(xiě)入內(nèi)容
sheet.write(0,0,'python')
sheet.write(1,1,'love')
# 保存文件到文件中
book.save('test.xls')
2.爬取起點(diǎn)中文網(wǎng)小說(shuō)信息,并存儲(chǔ)到Excel文件中。
思路分析: 1.url分析
我們的目標(biāo)url:http://a.qidian.com/
第一頁(yè):https://www.qidian.com/all/ 第二頁(yè):https://www.qidian.com/all/page2/
第三頁(yè):https://www.qidian.com/all/page3/ 第四頁(yè) :https://www.qidian.com/all/page4/ ... 看出規(guī)律都是由https://www.qidian.com/all/ +page{}/ 組成 所以第一頁(yè):https://www.qidian.com/all/page1/ 是可以成功的 所以u(píng)rl部分的代碼可以寫(xiě)為 urls = ['https://www.qidian.com/all/page{}/'.format(str(i)) for i in range(1,6)]
內(nèi)容分析: 根據(jù)我們要爬取內(nèi)容的要求爬取起點(diǎn)中文網(wǎng)的全部信息
先找大后找小,尋找循環(huán)點(diǎn)
如圖:
經(jīng)過(guò)實(shí)驗(yàn),這個(gè)循環(huán)點(diǎn)xpath路徑是不對(duì)的,正確的xpath路徑應(yīng)該是:
infos = html.xpath('//ul[@class="all-img-list cf"]/li')
for info in infos: ?? ?# 1.書(shū)名 ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/h2/a
bookname = info.xpath('div/h2/a/text()')[0]
?? ?#2.鏈接 ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/h2/a ?? ?book_link=info.xpath('div[2]/h2/a/@href')[0]
?? ? ?? ?# 3.作者 ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[1]/a[1] ?? ?name=info.xpath('div/p[1]/a[1]/text()')[0]
?? ? ?? ?# 4.書(shū)的種類 ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[1]/a[2] ?? ?book_kind=info.xpath('div[2]/p[1]/a[2]/text()')[0] ?? ? ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[1]/a[3] ?? ?boo_kind2=info.xpath('div[2]/p[1]/a[3]/text()')[0]
?? ?# 5.書(shū)的狀態(tài) ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[1] ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[1]/span ?? ?state=info.xpath('div[2]/p[1]/text()')[0] ?? ?
?? ?#6.書(shū)的簡(jiǎn)介 ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[2] ?? ? introduction=info.xpath('div[2]/p[2]/text()')[0] ?? ? ?? ?# 7.書(shū)的字?jǐn)?shù) ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[3]/span/span ?? ?num=info.xpath('div[2]/p[3]/span/span/text()')[0]
?? ?# 8.書(shū)的最新章節(jié) ?? ?//*[@id="book-img-text"]/ul/li[1]/div[2]/p[3]/span/a ?? ?new_chapter=info.xpath('div[2]/p[3]/span/a/text()')[0]
把這些分析出來(lái)差不多就可以寫(xiě)代碼了:
代碼
# 1.導(dǎo)包requests,lxml,xlwt
import unicodedata
import requests
from lxml import etree
import xlwt
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'}
# 2.建立一個(gè)函數(shù)def get_info():
def get_info(url):
# a.使用get方法發(fā)送get請(qǐng)求,獲取響應(yīng)
res = requests.get(url, headers=headers)
html = etree.HTML(res.text)
# b.先找大后找小,尋找循環(huán)點(diǎn)(循環(huán)點(diǎn))
# //*[@id="book-img-text"]/ul/li[1]
# ('//tr[@class="item"]')
infos = html.xpath('//ul[@class="all-img-list cf"]/li')
# 建立一個(gè)空列表
allinfo_list = []
# # c.使用for循環(huán),循環(huán)里是我們需要爬取的內(nèi)容
for info in infos:
bookname = info.xpath('div/h2/a/text()')[0]
book_link = info.xpath('div[2]/h2/a/@href')[0]
name = info.xpath('div/p[1]/a[1]/text()')[0]
book_kind = info.xpath('div[2]/p[1]/a[2]/text()')[0]
boo_kind2 = info.xpath('div[2]/p[1]/a[3]/text()')[0]
kind=book_kind+'.'+boo_kind2
state = info.xpath('div[2]/p[1]/span/text()')[0]
introduction = info.xpath('div[2]/p[2]/text()')[0]
# # 將每個(gè)字符的Unicode碼點(diǎn)組合成一個(gè)整數(shù)
# num = int(''.join(str(ord(c)) for c in text))
# # 使用字符串格式化將科學(xué)計(jì)數(shù)法轉(zhuǎn)換為十進(jìn)制形式
# decimal_num = '{:.0f}'.format(num)
number = info.xpath('div[2]/p[3]/span/span/text()')[0]
num1='{:.0f}'.format(int(''.join(str(ord(c)) for c in number)))
num= '{:.2f}百萬(wàn)字'.format(float(num1)/1000000.0)
new_chapter = info.xpath('div[2]/p[3]/span/a/text()')[0]
# 使用.append()把元組添加進(jìn)列表
all_info = [bookname, book_link, name,kind, state, introduction, num, new_chapter]
allinfo_list.append(all_info)
return allinfo_list
#
#
# # # 3.主程序
if __name__ == '__main__':
# a.urls
urls = ['https://www.qidian.com/all/page{}/'.format(str(i)) for i in range(1,6)]
# print(urls)
# b.for 循環(huán)取出url
# 通過(guò)Workbook()方法創(chuàng)建一個(gè)工作簿
book = xlwt.Workbook(encoding='UTF-8')
# 創(chuàng)建一個(gè)名字為Sheet1的工作表
sheet = book.add_sheet('Sheet1')
list = ['書(shū)名', '鏈接', '作者', '書(shū)的種類', '書(shū)的狀態(tài)', '書(shū)的簡(jiǎn)介', '字?jǐn)?shù)', '新的章節(jié)']
for index in range(len(list)):
sheet.write(0, index, list[index])
n=1
for url in urls:
data=get_info(url)
for i in data:
# 寫(xiě)入數(shù)據(jù),可以看出第一個(gè)和第二個(gè)參數(shù)為Excel表格的單元格位置,第三個(gè)寫(xiě)入內(nèi)容
for index1 in range(len(i)):
sheet.write(n, index1, i[index1])
n = n + 1
# 保存文件到文件中
book.save('qidian.xls')
柚子快報(bào)激活碼778899分享:第四個(gè)爬蟲(chóng)實(shí)戰(zhàn)代碼
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。