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

python自動從arxiv下載paper的示例代碼

 更新時間:2020年12月05日 10:26:09   作者:dangxusheng  
這篇文章主要介紹了python自動從arxiv下載paper的示例代碼,幫助大家更好的理解和學習python,感興趣的朋友可以了解下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time  : 2020/02/11 21:44
# @Author : dangxusheng
# @Email  : dangxusheng163@163.com
# @File  : download_by_href.py
'''
自動從arxiv.org 下載文獻
'''

import os
import os.path as osp
import requests
from lxml import etree
from pprint import pprint
import re
import time
import glob

headers = {
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36",
  "Host": 'arxiv.org'
}

HREF_CN = 'http://cn.arxiv.org/pdf/'
HREF_SRC = 'http://cn.arxiv.org/pdf/'
SAVE_PATH = '/media/dangxs/E/Paper/download_at_20200730'
os.makedirs(SAVE_PATH, exist_ok=True)

FAIL_URLS = []
FAIL_URLS_TXT = f'{SAVE_PATH}/fail_urls.txt'


def download(url, title):
  pattern = r'[\\/:*?"\'<>|\r\n]+'
  new_title = re.sub(pattern, " ", title)
  print(f'new title: {new_title}')
  save_filepath = '%s/%s.pdf' % (SAVE_PATH, new_title)
  if osp.exists(save_filepath) and osp.getsize(save_filepath) > 50 * 1024:
    print(f'this pdf is be existed.')
    return True
  try:
    with open(save_filepath, 'wb') as file:
      # 分字節(jié)下載
      r = requests.get(url, stream=True, timeout=None)
      for i in r.iter_content(2048):
        file.write(i)
    if osp.getsize(save_filepath) >= 10 * 1024:
      print('%s 下載成功.' % title)
      return True
  except Exception as e:
    print(e)
  return False


# 從arxiv.org 去下載
def search(start_size=0, title_keywords='Facial Expression'):
  # 訪問地址: https://arxiv.org/find/grp_eess,grp_stat,grp_cs,grp_econ,grp_math/1/ti:+Face/0/1/0/past,2018,2019/0/1?skip=200&query_id=1c582e6c8afc6146&client_host=cn.arxiv.org
  req_url = 'https://arxiv.org/search/advanced'
  req_data = {
    'advanced': 1,
    'terms-0-operator': 'AND',
    'terms-0-term': title_keywords,
    'terms-0-field': 'title',
    'classification-computer_science': 'y',
    'classification-physics_archives': 'all',
    'classification-include_cross_list': 'include',
    'date-filter_by': 'date_range', # date_range | specific_year
    # 'date-year': DOWN_YEAR,
    'date-year': '',
    'date-from_date': '2015',
    'date-to_date': '2020',
    'date-date_type': 'announced_date_first', # submitted_date | submitted_date_first | announced_date_first
    'abstracts': 'show',
    'size': 50,
    'order': '-announced_date_first',
    'start': start_size,
  }
  res = requests.get(req_url, params=req_data, headers=headers)
  html = res.content.decode()
  html = etree.HTML(html)

  total_text = html.xpath('//h1[@class="title is-clearfix"]/text()')
  total_text = ''.join(total_text).replace('\n', '').lstrip(' ').strip(' ')
  # i.e. : Showing 1–50 of 355 results
  num = re.findall('\d+', total_text)
  # Sorry, your query returned no results
  if len(num) == 0: return [], 0

  total = int(num[-1]) # 查詢總條數(shù)
  paper_list = html.xpath('//ol[@class="breathe-horizontal"]/li')
  info_list = []
  for p in paper_list:
    title = p.xpath('./p[@class="title is-5 mathjax"]//text()')
    title = ''.join(title).replace('\n', '').lstrip(' ').strip(' ')
    href = p.xpath('./div/p/a/@href')[0]
    info_list.append({'title': title, 'href': href})

  return info_list, total


# 去指定頁面下載
def search_special():
  res = requests.get('https://gitee.com/weberyoung/the-gan-zoo?_from=gitee_search')
  html = res.content.decode()
  html = etree.HTML(html)

  paper_list = html.xpath('//div[@class="file_content markdown-body"]//li')
  info_list = []
  for p in paper_list:
    title = p.xpath('.//text()')
    title = ''.join(title).replace('\n', '').lstrip(' ').strip(' ')
    href = p.xpath('./a/@href')[0]
    info_list.append({'title': title, 'href': href})

  pprint(info_list)
  return info_list


if __name__ == '__main__':
  page_idx = 0
  total = 1000
  keywords = 'Facial Action Unit'
  while page_idx <= total // 50:
    paper_list, total = search(page_idx * 50, keywords)
    print(f'total: {total}')
    if total == 0:
      print('no found .')
      exit(0)

    for p in paper_list:
      title = p['title']
      href = HREF_CN + p['href'].split('/')[-1] + '.pdf'
      print(href)
      if not download(href, title):
        print('從國內(nèi)鏡像下載失敗,從源地址開始下載 >>>>')
        # 使用國際URL再下載一次
        href = HREF_SRC + p['href'].split('/')[-1] + '.pdf'
        if not download(href, title):
          FAIL_URLS.append(p)
    page_idx += 1

  # 下載最后的部分
  last_1 = total - page_idx * 50
  paper_list, total = search(last_1, keywords)
  for p in paper_list:
    title = p['title']
    href = HREF_CN + p['href'].split('/')[-1] + '.pdf'
    if not download(href, title):
      FAIL_URLS.append(p)
    time.sleep(1)

  pprint(FAIL_URLS)
  with open(FAIL_URLS_TXT, 'a+') as f:
    for item in FAIL_URLS:
      href = item['href']
      title = item['title']
      f.write(href + '\n')

  print('done.')

以上就是python自動從arxiv下載paper的示例代碼的詳細內(nèi)容,更多關(guān)于python 從arxiv下載paper的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python設(shè)計模式之觀察者模式簡單示例

    Python設(shè)計模式之觀察者模式簡單示例

    這篇文章主要介紹了Python設(shè)計模式之觀察者模式,簡單描述了觀察者模式的概念、原理,并結(jié)合實例形式分析了Python觀察者模式的相關(guān)定義與使用技巧,需要的朋友可以參考下
    2018-01-01
  • Python爬蟲爬取新聞資訊案例詳解

    Python爬蟲爬取新聞資訊案例詳解

    這篇文章主要介紹了Python爬蟲爬取新聞資訊案例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Python自動化測試筆試面試題精選

    Python自動化測試筆試面試題精選

    在本篇文章里小編給大家整理的是一篇關(guān)于Python自動化測試筆試面試時常見的編程題,需要的朋友們可以學習參考下。
    2020-03-03
  • Python開發(fā)網(wǎng)站目錄掃描器的實現(xiàn)

    Python開發(fā)網(wǎng)站目錄掃描器的實現(xiàn)

    這篇文章主要介紹了Python開發(fā)網(wǎng)站目錄掃描器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02
  • Python列表數(shù)據(jù)如何按區(qū)間分組統(tǒng)計各組個數(shù)

    Python列表數(shù)據(jù)如何按區(qū)間分組統(tǒng)計各組個數(shù)

    這篇文章主要介紹了Python列表數(shù)據(jù)如何按區(qū)間分組統(tǒng)計各組個數(shù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Python+Scipy實現(xiàn)自定義任意的概率分布

    Python+Scipy實現(xiàn)自定義任意的概率分布

    Scipy自帶了多種常見的分布,如正態(tài)分布、均勻分布、二項分布、多項分布、伽馬分布等等,還可以自定義任意的概率分布。本文將為大家介紹如何利用Scipy自定義任意的概率分布,感興趣的可以了解下
    2022-08-08
  • python線程join方法原理解析

    python線程join方法原理解析

    這篇文章主要介紹了python線程join方法原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 一文帶你掌握Python自然語言處理庫SpaCy

    一文帶你掌握Python自然語言處理庫SpaCy

    SpaCy是一個非常強大的Python自然語言處理庫,它包含了眾多強大功能,如詞性標注、命名實體識別、依賴關(guān)系解析等等,這篇文章的目標是幫助你了解SpaCy的基本功能和如何使用,需要的朋友可以參考下
    2023-07-07
  • Python 正則表達式入門(初級篇)

    Python 正則表達式入門(初級篇)

    本文主要為沒有使用正則表達式經(jīng)驗的新手入門所寫。由淺入深介紹了Python 正則表達式,有需要的朋友可以看下
    2016-12-12
  • Python實現(xiàn)在線批量美顏功能過程解析

    Python實現(xiàn)在線批量美顏功能過程解析

    這篇文章主要介紹了Python實現(xiàn)在線批量美顏功能過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論

射阳县| 陵水| 永善县| 汝州市| 襄垣县| 仁怀市| 荆门市| 平罗县| 都兰县| 腾冲县| 彰化市| 莱州市| 斗六市| 信丰县| 浙江省| 噶尔县| 海淀区| 瓮安县| 大安市| 山阴县| 枞阳县| 邵东县| 朔州市| 大荔县| 北安市| 永安市| 六枝特区| 海盐县| 买车| 宾川县| 许昌县| 克山县| 沈丘县| 吉林市| 萝北县| 哈巴河县| 灵璧县| 来凤县| 西盟| 全南县| 通海县|