最近导师叫我下载CVPR,ICCV,ECCV会议论文,发现每个期刊都有好几百篇论文。一个一个点击非常耗费时间,正好在学习爬虫,利用爬虫抓取一下网页。
完成老师分配的任务之后将这个代码分享出来,供大家使用和学习,解决下载论文的烦恼。毕竟科技的进步就是使人懒惰,哈哈哈
运行速度和每个人的网速有关,可以先在浏览器中下载一个论文,测一下网速,网速慢的话建议搭载VPN,也有部分论文内存很大,可以中断程序,for循环跳过即可。
我们将提取的PDF链接自动下载论文,然后保存在文件夹中。
1.分析网页
首先打开三个会议期刊的接口
:
https://openaccess.thecvf.com/menu
我们可以清楚的看到期刊按照年份进行排序,其中ECCV在Other Computer Vision Conferences and Workshops中。
在得到每个期刊每年的路径接口后,我们就可以得到该网址的URL,下面我们将演示对三个期刊的爬取。
2.分析ECCV期刊页面
ECCV中罗列出2020年和2018年份论文接口,我们选择一个查看页面。
网站的的URL为:
https://openaccess.thecvf.com/ECCV2018
因此当需要爬取2020年份的论文只需要把地址改从:
https://openaccess.thecvf.com/ECCV2018
改为:
https://openaccess.thecvf.com/ECCV2020
点击网页上PDF按钮,浏览器自动下载对应的论文,我们的目的是自动下载论文,因此只需要提取出PDF对应的下载链接即可。顺便把论文的标题爬取下来,当做保存文件的名称方便查看
3.爬取ECCV期刊代码
在上一部中我们分析了网页,可以得到网页的URL和title。
在此基础上,网页的加载是JS渲染,因此我们不能简单的通过request.get()获取网页的HTML代码。需要使用selenium驱动浏览器获得page_source。
在代码的编写上,使用for循环遍历读取的URL,方便论文内存大时,跳过该论文。
此外爬取的链接是不完整的,我们 还需要加上:
https://openaccess.thecvf.com/
import requests,re,os,json
from pyquery import PyQuery as pq
from lxml import etree
import time
from selenium import webdriver
def gethtml(url):
brower = webdriver.Firefox()
brower.get(url)
html = brower.page_source
return html
def getpdf(html):
html=etree.HTML(html)
indexs=html.xpath('//dl/dd/a[1]/@href')
base_url='https://openaccess.thecvf.com/'
title=html.xpath('//dl/dt/a/text()')
print(len(title))
for i in range(0,len(title)):
url=base_url+indexs[i]
print(url)
#print(title[i])
writepdf(url,title[i])
def writepdf(url,title):
response=requests.get(url)
file_path='C:/Users/10043/Desktop/ECCV2018'
PDF_path=file_path+os.path.sep+'{0}.{1}'.format(title.replace(':','').replace('?',''),'pdf')
if not os.path.exists(PDF_path):
with open(PDF_path,'wb') as f:
print('正在抓取:'+title)
f.write(response.content)
#time.sleep(1)
f.close()
else:
print('已下载: '+title)
if __name__=='__main__':
url='https://openaccess.thecvf.com/ECCV2018'
html=gethtml(url)
getpdf(html)
4.爬取ICCV期刊代码
ICCV是按照日期索引的,因此改变URL即可
ICCV中多了一个go back按钮,因此将提取的URL索引从1开始,剔除go back链接。
下面是完整的代码
import requests,re,os,json
from pyquery import PyQuery as pq
from lxml import etree
import time
from selenium import webdriver
def gethtml(url):
brower = webdriver.Firefox()
brower.get(url)
html = brower.page_source
return html
def getpdf(html):
html=etree.HTML(html)
indexs=html.xpath('//dl/dd/a[1]/@href')
base_url='https://openaccess.thecvf.com/'
title=html.xpath('//dl/dt/a/text()')
print(len(title))
for i in range(0,len(title)):
url=base_url+indexs[i+1]
print(url)
#print(title[i])
writepdf(url,title[i])
def writepdf(url,title):
response=requests.get(url)
file_path='C:/Users/10043/Desktop/ICCV2018'
PDF_path=file_path+os.path.sep+'{0}.{1}'.format(title.replace(':','').replace('?',''),'pdf')
if not os.path.exists(PDF_path):
with open(PDF_path,'wb') as f:
print('正在抓取:'+title)
f.write(response.content)
#time.sleep(1)
f.close()
else:
print('已下载: '+title)
if __name__=='__main__':
url='https://openaccess.thecvf.com/ICCV2019?day=2019-10-30'
html=gethtml(url)
getpdf(html)
5.爬取CVPR期刊代码
CVPR和ICCV网页分布相同,只需要更改URL,可以把ICCV的代码用在抓取CVPR上
将
https://openaccess.thecvf.com/ICCV2019?day=2019-10-30
改为:
https://openaccess.thecvf.com/CVPR2019?day=2019-06-18
其实只需要将ICCV2019和day=2019-10-30改为CVPR对应的格式即可。
6.结果展示
其实在提取title时,可以添加字符串判断语句,只对自己感兴趣的论文下载,这样就可以提取自己想要的文件,例如图像去噪,超分辨率,图像分割等。