免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Python爬取某網(wǎng)站文檔數(shù)據(jù)完整教程(附源碼)

前言

本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,如有問題請及時(shí)聯(lián)系我們以作處理。

基本開發(fā)環(huán)境

Python 3.6

Pycharm

相關(guān)模塊的使用

import osimport requestsimport timeimport reimport jsonfrom docx import Documentfrom docx.shared import Cm

安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。


目標(biāo)網(wǎng)頁分析

網(wǎng)站的文檔內(nèi)容,都是以圖片形式存在的。它有自己的數(shù)據(jù)接口

接口鏈接:

https://openapi.book118.com/getPreview.html?&project_id=1&aid=272112230&t=f2c66902d6b63726d8e08b557fef90fb&view_token=SqX7ktrZ_ZakjDI@vcohcCwbn_PLb3C1&page=1&callback=jQuery18304186406662159248_1614492889385&_=1614492889486

接口的請求參數(shù)

整體思路

  • 請求網(wǎng)頁返回response數(shù)據(jù)(字符串)

  • 通過re模塊匹配提取中間的數(shù)據(jù)(列表)索引取0(字符串)

  • 通過json模塊是把提取出來的數(shù)據(jù)轉(zhuǎn)換成json模塊

  • 通過遍歷獲取每張圖片的url地址

  • 保存圖片到本地文件夾

  • 把圖片保存到word文檔

  • 爬蟲代碼實(shí)現(xiàn)

爬蟲代碼實(shí)現(xiàn)

def download():
    content = 0for page in range(1, 96, 6):# 給定 2秒延時(shí)time.sleep(2)# 獲取時(shí)間戳now_time = int(time.time() * 1000)
        url = 'https://openapi.book118.com/getPreview.html'# 請求參數(shù)params = {'project_id': '1','aid': '272112230','t': 'f2c66902d6b63726d8e08b557fef90fb','view_token': 'SqX7ktrZ_ZakjDI@vcohcCwbn_PLb3C1','page': f'{page}','_': now_time,
        }# 請求頭headers = {'Host': 'openapi.book118.com','Referer': 'https://max.book118.com/html/2020/0427/8026036013002110.shtm','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
        response = requests.get(url=url, params=params, headers=headers)# 使用正則表達(dá)式提取內(nèi)容result = re.findall('jsonpReturn\((.*?)\)', response.text)[0]# 字符串轉(zhuǎn)json數(shù)據(jù)json_data = json.loads(result)['data']# 字典值的遍歷for value in json_data.values():
            content += 1# 拼接圖片urlimg_url = 'http:' + valueprint(img_url)
            headers_1 = {'Host': 'view-cache.book118.com','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}# 請求圖片url地址 獲取content二進(jìn)制數(shù)據(jù)img_content = requests.get(url=img_url, headers=headers_1).content# 文件名img_name = str(content) + '.jpg'# 保存路徑filename = 'img\\'# 以二進(jìn)制方式保存 (圖片、音頻、視頻等文件都是以二進(jìn)制的方式保存)with open(filename + img_name, mode='wb') as f:
                f.write(img_content)

注意點(diǎn):

1、一定要給延時(shí),不然后面接口數(shù)據(jù)會(huì)請求不到。

2、請求圖片url的時(shí)候headers參數(shù)需要寫完整,否則保存圖片是無法打開的

3、命名最好是給定數(shù)字,1.jpg、2.jpg 這樣,方便后續(xù)保存到word

爬蟲部分的代碼還是比較簡單的,沒有什么特別的難度。

爬取這些文檔,都是需要打印或者查詢所以要把這些單張的圖片都保存到word文檔里面。

寫入文檔

def save_picture():
    document = Document()
    path = './img/'lis = os.listdir(path)
    c = []for li in lis:
        index = li.replace('.jpg', '')
        c.append(index)
    c_1 = sorted(list(map(int, c)))print(c_1)
    new_files = [(str(i) + '.jpg') for i in c_1]for num in new_files:
        img_path = path + num
        document.add_picture(img_path, width=Cm(17), height=Cm(24))
        document.save('tu.doc')  # 保存文檔os.remove(img_path)  # 刪除保存在本地的圖片
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
想要音頻數(shù)據(jù)卻不好批量下載怎么弄?python教你一招解決~
Python|爬取短視頻
年關(guān)將至,用Python來爬取股票數(shù)據(jù)進(jìn)行分析,沖一波!
python爬蟲整理| 原來我曾經(jīng)擁有過這么多爬蟲啊
【python爬蟲】80頁md筆記第(4)篇:requests和網(wǎng)絡(luò)數(shù)據(jù)獲取進(jìn)階
原來沒有下載券,也可以下載百度文庫PPT,Python確實(shí)牛
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服