热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用python获取nature系列期刊封面高清图片

nature作为科学界最顶级的期刊之一,其期刊封面审美也一直很在线,兼具科学和艺术的美感为了方便快速获取nature系列封面,这里用py

nature作为科学界最顶级的期刊之一,其期刊封面审美也一直很在线,兼具科学和艺术的美感

为了方便快速获取nature系列封面,这里用python requests模块进行自动化请求并使用BeautifulSoup模块进行html解析

import requests
from bs4 import BeautifulSoup
import ospath = 'C:\\Users\\User\\Desktop\\nature 封面\\nature 正刊'
# path = os.getcwd()
if not os.path.exists(path):os.makedirs(path)print("新建文件夹 nature正刊")# 在这里改变要下载哪期的封面
# 注意下载是从后往前下载的,所以start_volume应大于等于end_volume
start_volume = 501
end_volume = 500
# nature_url = 'https://www.nature.com/ng/volumes/' # nature genetics
nature_url='https://www.nature.com/nature/volumes/' # nature 正刊
kv = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
while start_volume >= end_volume:try:volume_url = nature_url + str(start_volume)volume_response = requests.get(url=volume_url, headers=kv, timeout=120)except Exception:print(str(start_volume) + "请求异常")with open(path + "\\异常.txt", 'at') as txt:txt.write(str(start_volume) + "请求异常\n")continuevolume_response.encoding = 'utf-8'volume_soup = BeautifulSoup(volume_response.text, 'html.parser')ul_tag = volume_soup.find_all('ul',class_='ma0 clean-list grid-auto-fill grid-auto-fill-w220 very-small-column medium-row-gap')img_list = ul_tag[0].find_all("img")issue_number = 0for img_tag in img_list:issue_number += 1filename = path + '\\' + str(start_volume) + '_' + str(issue_number) + '.png'if os.path.exists(filename):print(filename + "已经存在")continueprint("Loading...........................")img_url = 'https:' + img_tag.get("src").replace("w200", "w1000")try:img_response = requests.get(img_url, timeout=240, headers=kv)except Exception:print(start_volume, issue_number, '???????????异常????????')with open(path + "\\异常.txt", 'at') as txt:txt.write(str(start_volume) + '_' + str(issue_number) + "请求异常\n")continuewith open(filename, 'wb') as imgfile:imgfile.write(img_response.content)print("成功下载图片:" + str(start_volume) + '_' + str(issue_number))start_volume -= 1

运行结果:

以上部分代码可以自动下载nature和nature genetics的封面,这两个期刊的网站结构跟其他子刊略有不同,其他子刊可以用以下代码来进行爬虫:

import requests
from bs4 import BeautifulSoup
import osother_journals = {'nature biomedical engineering': 'natbiomedeng','nature methods': 'nmeth','nature astronomy': 'natastron','nature medicine': 'nm','nature protocols': 'nprot','nature microbiology': 'nmicrobiol','nature cell biology': 'ncb','nature nanotechnology': 'nnano','nature immunology': 'ni','nature energy': 'nenergy','nature materials': 'nmat','nature cancer': 'natcancer','nature neuroscience': 'neuro','nature machine intelligence': 'natmachintell','nature metabolism': 'natmetab','nature food': 'natfood','nature ecology & evolution': "natecolevol","nature stuctural & molecular biology":"nsmb","nature physics":"nphys","nature human behavior":"nathumbehav","nature chemical biology":"nchembio"
}nature_journal = {# 要下载的期刊放这里'nature plants': 'nplants','nature biotechnology': 'nbt'
}
folder_Name = "nature 封面"
kv = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}def makefile(path):folder = os.path.exists(path)if not folder:os.makedirs(path)print("Make file -- " + path + " -- successfully!")else:raise AssertionError################################################################
def getCover(url, journal, year, filepath, startyear&#61;2022, endyear&#61;2022):# 注意endyear是比startyear小的,因为是从endyear开始由后往前来下载的if not (endyear <&#61; year <&#61; startyear):returntry:issue_response &#61; requests.get("https://www.nature.com" &#43; url,timeout&#61;120,headers&#61;kv)except Exception:print(journal &#43; " " &#43; str(year) &#43; " Error")returnissue_response.encoding &#61; &#39;gbk&#39;if &#39;Page not found&#39; in issue_response.text:print(journal &#43; " Page not found")returnissue_soup &#61; BeautifulSoup(issue_response.text, &#39;html.parser&#39;)cover_image &#61; issue_soup.find_all("img", class_&#61;&#39;image-constraint pt10&#39;)for image in cover_image:image_url &#61; image.get("src")print("Start loading img.............................")image_url &#61; image_url.replace("w200", "w1000")if (image_url[-2] &#61;&#61; &#39;/&#39;):month &#61; "0" &#43; image_url[-1]else:month &#61; image_url[-2:]image_name &#61; nature_journal[journal] &#43; "_" &#43; str(year) &#43; "_" &#43; month &#43; ".png"if os.path.exists(filepath &#43; journal &#43; "\\" &#43; image_name):print(image_url &#43; " 已经存在")continueprint(image_url)try:image_response &#61; requests.get("http:" &#43; image_url,timeout&#61;240,headers&#61;kv)except Exception:print("获取图片异常:" &#43; image_name)continuewith open(filepath &#43; journal &#43; "\\" &#43; image_name,&#39;wb&#39;) as downloaded_img:downloaded_img.write(image_response.content)def main():try:path &#61; os.getcwd() &#43; &#39;\\&#39;makefile(path &#43; folder_Name)except Exception:print("文件夹 --nature 封面-- 已经存在")path &#61; path &#43; folder_Name &#43; "\\"for journal in nature_journal:try:makefile(path &#43; journal)except AssertionError:print("File -- " &#43; path &#43; " -- has already exist!")try:volume_response &#61; requests.get("https://www.nature.com/" &#43;nature_journal[journal] &#43;"/volumes",timeout&#61;120,headers&#61;kv)except Exception:print(journal &#43; " 异常")continuevolume_response.encoding &#61; &#39;gbk&#39;volume_soup &#61; BeautifulSoup(volume_response.text, &#39;html.parser&#39;)volume_list &#61; volume_soup.find_all(&#39;ul&#39;,class_&#61;&#39;clean-list ma0 clean-list grid-auto-fill medium-row-gap background-white&#39;)number_of_volume &#61; 0for volume_child in volume_list[0].children:if volume_child &#61;&#61; &#39;\n&#39;:continueissue_url &#61; volume_child.find_all("a")[0].get("href")print(issue_url)print(2020 - number_of_volume)getCover(issue_url,journal,year&#61;(2020 - number_of_volume),filepath&#61;path,startyear&#61;2022, endyear&#61;2022)number_of_volume &#43;&#61; 1if __name__ &#61;&#61; "__main__":main()print("Finish Everything!")

运行结果&#xff1a;


推荐阅读
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • 使用正则表达式爬取36Kr网站首页新闻的操作步骤和代码示例
    本文介绍了使用正则表达式来爬取36Kr网站首页所有新闻的操作步骤和代码示例。通过访问网站、查找关键词、编写代码等步骤,可以获取到网站首页的新闻数据。代码示例使用Python编写,并使用正则表达式来提取所需的数据。详细的操作步骤和代码示例可以参考本文内容。 ... [详细]
  • 文章目录简介HTTP请求过程HTTP状态码含义HTTP头部信息Cookie状态管理HTTP请求方式简介HTTP协议(超文本传输协议)是用于从WWW服务 ... [详细]
  • 最近在学Python,看了不少资料、视频,对爬虫比较感兴趣,爬过了网页文字、图片、视频。文字就不说了直接从网页上去根据标签分离出来就好了。图片和视频则需要在获取到相应的链接之后取做下载。以下是图片和视 ... [详细]
  • 延迟注入工具(python)的SQL脚本
    本文介绍了一个延迟注入工具(python)的SQL脚本,包括使用urllib2、time、socket、threading、requests等模块实现延迟注入的方法。该工具可以通过构造特定的URL来进行注入测试,并通过延迟时间来判断注入是否成功。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 本文介绍了使用Python解析C语言结构体的方法,包括定义基本类型和结构体类型的字典,并提供了一个示例代码,展示了如何解析C语言结构体。 ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
  • ECMA262规定typeof操作符的返回值和instanceof的使用方法
    本文介绍了ECMA262规定的typeof操作符对不同类型的变量的返回值,以及instanceof操作符的使用方法。同时还提到了在不同浏览器中对正则表达式应用typeof操作符的返回值的差异。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 这个问题发生在重新安装系统后,丢失了之前的privatekey等。所以解决方法就是提示的revokeandrequest。到developercenter中找到certificat ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
author-avatar
陈可不能哭
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有