使用python爬虫抓取学术论文
2015-03-25 20:19
2146 查看
介绍
这是一个很小的爬虫,可以用来爬取学术引擎的pdf论文,由于是网页内容是js生成的,所以必须动态抓取。通过selenium和chromedriver实现。可以修改起始点的URL从谷粉搜搜改到谷歌学术引擎,如果你的电脑可以翻墙。可以修改关键字 和 搜索页数 搜索需要的论文资源下载
selenium和chromedriver,2015-3月最新版本下载地址 http://pan.baidu.com/s/1qWLqqqK注意运行程序前 启动selenium 命令为 java -jar selenium.jar
python代码
#!/usr/bin/python #encoding=utf-8 __author__ = 'Administrator' from selenium import selenium if __name__ == "__main__": import os from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait chromedriver = "C:\Program Files\Google\Chrome\Application\chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") driver.get('http://www.gfsoso.com/scholar') inputElement = driver.find_element_by_name("q") searchWord="sentiment lexicon" inputElement.send_keys(searchWord) inputElement.submit() currentURL=driver.current_url urlList=[] localDir = 'down_pdf\\' fileOut = localDir + searchWord + ".txt" import urllib, re,codecs,sys fileOp = codecs.open(fileOut, 'a', sys.getdefaultencoding()) for i in range(0,10):#需要抓取的页数 pdf_url = driver.find_elements_by_css_selector("a") for k in pdf_url: try: z= k.get_attribute("href") if '.pdf' in z and z not in urlList: urlList.append(z) print z except: import time time.sleep(1) continue contents=driver.find_elements_by_css_selector('h3') for ct in contents: print ct.text #fileOp.write('%s\n' %(ct.text))#把页面上所有的文章名称存到txt,有时会报错 driver.get(currentURL+"&start="+str(i*10)+"&as_sdt=0,5&as_ylo=2008") import time time.sleep(3) print len(urlList) for everyURL in urlList: #遍历列表的每一项,即每一个PDF的url wordItems = everyURL.split('/') #将url以/为界进行划分,为了提取该PDF文件名 for item in wordItems: #遍历每个字符串 if re.match('.*\.pdf$', item): #查找PDF的文件名 PDFName = item #查找到PDF文件名 localPDF = localDir +searchWord+"_"+ PDFName try: urllib.urlretrieve(everyURL, localPDF) #按照url进行下载,并以其文件名存储到本地目录 except Exception,e: continue
相关文章推荐
- Python抓取优酷视频(上):爬虫使用及数据处理
- 使用python/casperjs编写终极爬虫-客户端App的抓取
- 使用python/casperjs编写终极爬虫-客户端App的抓取
- 使用python/casperjs编写终极爬虫-客户端App的抓取
- 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
- 零基础写python爬虫之使用urllib2组件抓取网页内容
- 使用Python编写简单网络爬虫抓取视频下载资源
- 使用Python编写简单网络爬虫抓取视频下载资源
- 零基础写python爬虫之使用urllib2组件抓取网页内容
- 使用Python编写简单网络爬虫抓取视频下载资源
- 使用python 编写 抓取内涵段子动态图的简单爬虫
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
- 使用python/casperjs编写终极爬虫-客户端App的抓取
- 使用python爬虫抓取页面之后,将页面保存到Mysql数据库中
- 使用Python编写简单网络爬虫抓取视频下载资源
- python爬虫"Hello World"级入门实例(二),使用json从中国天气网抓取数据
- python爬虫之使用urllib2组件抓取网页内容
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
- 使用 Python 编写多线程爬虫抓取百度贴吧邮箱与手机号