最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 爬取百度文庫并下載(免費(fèi)文章限定)

 更新時(shí)間:2020年12月04日 08:41:58   作者:Jack-Cherish  
這篇文章主要介紹了python 爬取百度文庫并下載的示例,幫助大家更好的理解和學(xué)習(xí)python 爬蟲的相關(guān)知識(shí),感興趣的朋友可以了解下
import requests
import re
import json
import os

session = requests.session()


def fetch_url(url):
 return session.get(url).content.decode('gbk')


def get_doc_id(url):
 return re.findall('view/(.*).html', url)[0]


def parse_type(content):
 return re.findall(r"docType.*?\:.*?\'(.*?)\'\,", content)[0]


def parse_title(content):
 return re.findall(r"title.*?\:.*?\'(.*?)\'\,", content)[0]


def parse_doc(content):
 result = ''
 url_list = re.findall('(https.*?0.json.*?)\\\\x22}', content)
 url_list = [addr.replace("\\\\\\/", "/") for addr in url_list]
 for url in url_list[:-5]:
  content = fetch_url(url)
  y = 0
  txtlists = re.findall('"c":"(.*?)".*?"y":(.*?),', content)
  for item in txtlists:
   if not y == item[1]:
    y = item[1]
    n = '\n'
   else:
    n = ''
   result += n
   result += item[0].encode('utf-8').decode('unicode_escape', 'ignore')
 return result


def parse_txt(doc_id):
 content_url = 'https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=' + doc_id
 content = fetch_url(content_url)
 md5 = re.findall('"md5sum":"(.*?)"', content)[0]
 pn = re.findall('"totalPageNum":"(.*?)"', content)[0]
 rsign = re.findall('"rsign":"(.*?)"', content)[0]
 content_url = 'https://wkretype.bdimg.com/retype/text/' + doc_id + '?rn=' + pn + '&type=txt' + md5 + '&rsign=' + rsign
 content = json.loads(fetch_url(content_url))
 result = ''
 for item in content:
  for i in item['parags']:
   result += i['c'].replace('\\r', '\r').replace('\\n', '\n')
 return result


def parse_other(doc_id):
 content_url = "https://wenku.baidu.com/browse/getbcsurl?doc_id=" + doc_id + "&pn=1&rn=99999&type=ppt"
 content = fetch_url(content_url)
 url_list = re.findall('{"zoom":"(.*?)","page"', content)
 url_list = [item.replace("\\", '') for item in url_list]
 if not os.path.exists(doc_id):
  os.mkdir(doc_id)
 for index, url in enumerate(url_list):
  content = session.get(url).content
  path = os.path.join(doc_id, str(index) + '.jpg')
  with open(path, 'wb') as f:
   f.write(content)
 print("圖片保存在" + doc_id + "文件夾")


def save_file(filename, content):
 with open(filename, 'w', encoding='utf8') as f:
  f.write(content)
  print('已保存為:' + filename)


# test_txt_url = 'https://wenku.baidu.com/view/cbb4af8b783e0912a3162a89.html?from=search'
# test_ppt_url = 'https://wenku.baidu.com/view/2b7046e3f78a6529657d5376.html?from=search'
# test_pdf_url = 'https://wenku.baidu.com/view/dd6e15c1227916888586d795.html?from=search'
# test_xls_url = 'https://wenku.baidu.com/view/eb4a5bb7312b3169a551a481.html?from=search'
def main():
 url = input('請(qǐng)輸入要下載的文庫URL地址')
 content = fetch_url(url)
 doc_id = get_doc_id(url)
 type = parse_type(content)
 title = parse_title(content)
 if type == 'doc':
  result = parse_doc(content)
  save_file(title + '.txt', result)
 elif type == 'txt':
  result = parse_txt(doc_id)
  save_file(title + '.txt', result)
 else:
  parse_other(doc_id)


if __name__ == "__main__":
 main()

爬取結(jié)果

以上就是python 爬取百度文庫并以下載的詳細(xì)內(nèi)容,更多關(guān)于python 爬取百度文庫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 創(chuàng)建子進(jìn)程模塊subprocess詳解

    Python 創(chuàng)建子進(jìn)程模塊subprocess詳解

    這篇文章主要介紹了Python 創(chuàng)建子進(jìn)程模塊subprocess詳解,本文詳細(xì)講解了subprocess模塊的方法、參數(shù)、使用實(shí)例等,需要的朋友可以參考下
    2015-04-04
  • Django Form 實(shí)時(shí)從數(shù)據(jù)庫中獲取數(shù)據(jù)的操作方法

    Django Form 實(shí)時(shí)從數(shù)據(jù)庫中獲取數(shù)據(jù)的操作方法

    這篇文章主要介紹了Django Form 實(shí)時(shí)從數(shù)據(jù)庫中獲取數(shù)據(jù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • Python基礎(chǔ)教程之pip的安裝和卸載

    Python基礎(chǔ)教程之pip的安裝和卸載

    pip是一種工具,可以幫助用戶安裝卸載一些需要的安裝包,非常的簡單實(shí)用,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)教程之pip的安裝和卸載的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • python cv2在驗(yàn)證碼識(shí)別中應(yīng)用實(shí)例解析

    python cv2在驗(yàn)證碼識(shí)別中應(yīng)用實(shí)例解析

    這篇文章主要介紹了python cv2在驗(yàn)證碼識(shí)別中應(yīng)用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • python使用Plotly繪圖工具繪制水平條形圖

    python使用Plotly繪圖工具繪制水平條形圖

    這篇文章主要為大家詳細(xì)介紹了python使用Plotly繪圖工具繪制水平條形圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 解決python報(bào)錯(cuò)MemoryError的問題

    解決python報(bào)錯(cuò)MemoryError的問題

    今天小編就為大家分享一篇解決python報(bào)錯(cuò)MemoryError的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式

    citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式

    這篇文章主要介紹了citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python中的time模塊和calendar模塊

    Python中的time模塊和calendar模塊

    這篇文章主要介紹了Python中的time模塊和calendar模塊,在Python中對(duì)時(shí)間和日期的處理方式有很多,其中轉(zhuǎn)換日期是最常見的一個(gè)功能。Python中的時(shí)間間隔是以秒為單位的浮點(diǎn)小數(shù)。下面來看看文章具體內(nèi)容的介紹,需要的朋友可以參考一下,希望對(duì)你有所幫助
    2021-11-11
  • 如何使用python編寫一個(gè)簡單的課時(shí)記錄系統(tǒng)

    如何使用python編寫一個(gè)簡單的課時(shí)記錄系統(tǒng)

    編寫一個(gè)應(yīng)用系統(tǒng)需要多方面的知識(shí)和技能,下面這篇文章主要給大家介紹了關(guān)于如何使用python編寫一個(gè)簡單的課時(shí)記錄系統(tǒng)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-04-04
  • Python如何使用struct.unpack處理二進(jìn)制文件

    Python如何使用struct.unpack處理二進(jìn)制文件

    這篇文章主要介紹了Python如何使用struct.unpack處理二進(jìn)制文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評(píng)論

抚松县| 沙洋县| 上饶县| 长宁县| 和林格尔县| 黑龙江省| 石门县| 永清县| 罗定市| 徐水县| 平谷区| 惠安县| 张家界市| 纳雍县| 木兰县| 信宜市| 长沙县| 五台县| 宾阳县| 洛阳市| 汝南县| 台北市| 开化县| 汝阳县| 临澧县| 福州市| 昌邑市| 天峻县| 重庆市| 惠州市| 闵行区| 十堰市| 扶绥县| 右玉县| 泰和县| 邵阳县| 朝阳区| 行唐县| 开封县| 梁山县| 高州市|