Python 爬取外文期刊论文信息(机械 仪表工业)

NSTL国家科技图书文献中心    2017  机械 仪表工业  所有期刊论文信息

代码比较随意,不要介意

第一步,爬取所有期刊链接

#coding=utf-8

import time
from selenium import webdriver
from lxml import etree
from pymongo import MongoClient

client = MongoClient("IP", 27017)
db = client["nstl"]
collection=db["journal_urls"]
db.authenticate("","")

driver = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver.exe")
driver.get(‘https://www.nstl.gov.cn/facade/search/clcSearch.do?&lan=eng&clc=TH‘)

html = driver.page_source
tree = etree.HTML(html)
count = int(tree.xpath("//span[@id=‘totalPages1‘]/text()")[0])

# 共47页
for i in range(count):

    html = driver.page_source
    tree = etree.HTML(html)

    # 提取当前页所有期刊链接并存储
    table = tree.xpath("//div[@class=‘s2listtd2‘]/span/a/@href")
    for j in table:
        bson = {}
        bson[‘url‘] = j
        collection.insert(bson)

    # i等于46时终止
    if i==(count-1):
        break

    # 点击接下来一页按钮
    driver.find_element_by_xpath(‘//div[@id="page"]/div//a[text()="%s"]‘%str(i+2)).click()

    # 判断翻页成功后跳出while
    while True:
        time.sleep(1)
        if driver.page_source!=html:
            break

driver.close()

第二步,爬取每个期刊中所有2017年论文链接

#coding=utf-8
import requests
from pymongo import MongoClient
from lxml import etree
from selenium import webdriver
import time

client = MongoClient("IP", 27017)
db = client["nstl"]
collection1=db["journal_urls"]
collection2=db["journalArticle2017_urls"]
db.authenticate("","")
driver = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver.exe")
# 循环所有期刊链接
for item in collection1.find({}, {"url":1, "_id":0}):
    driver.get(item[‘url‘][29:-4])
    html = driver.page_source
    tree = etree.HTML(html)
    # 判断如果有18年论文,需要点击出17年论文
    table_2018 = tree.xpath("//div[@id=‘year_2018‘]")
    if table_2018!=[]:
        driver.find_element_by_xpath("//div[@id=‘year_2017‘]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//div[@id=‘volumeUl_2017‘]/div[@class=‘ltreebom2‘]").click()
    # 获取17年期的个数并循环
    table = tree.xpath("//div[@id=‘volumeUl_2017‘]//div[@class=‘ltreebom3‘]/a")
    for i in range(1, len(table)+1):
        wen_html = driver.page_source
        wen_tree = etree.HTML(wen_html)
        # 获取当前一期的所有论文链接
        wen_table = tree.xpath("//div[@class=‘s2listtd2‘]/a/@href")
        for j in wen_table:
            bson = {}
            bson[‘url‘] = j
            collection2.insert(bson)
        # 判断结束循环
        if i==len(table):
            break
        # 点击出下一期论文
        try:
            driver.find_element_by_xpath("//div[@id=‘volumeUl_2017‘]//div[@class=‘ltreebom3‘][%s]"%str(i+1)).click()
        except:
            break
        # 判断是否点击成功
        while True:
            time.sleep(1)
            if driver.page_source!=wen_html:
                break

driver.close()

第三步,爬取论文信息详情页源码

#coding=utf-8
import requests
from pymongo import MongoClient
from lxml import etree
from selenium import webdriver
import time

client = MongoClient("IP", 27017)
db = client["nstl"]
collection=db["journalArticle2017_urls"]
collection1=db["journalArticle2017_codes"]
db.authenticate("","")

driver = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver.exe")

# 循环所有论文并构造链接
for item in collection.find({}, {"url":1, "_id":0}):

    url = "https://www.nstl.gov.cn/facade/search/toFullView.do?checkedSEQNO="+item[‘url‘][23:-11]+"&subDocType="+item[‘url‘][-8:-3]

    # # post方法获取当前页源码
    # for i in range(100):
    #     try:
    #         result = requests.post(url, verify = False)
    #     except:
    #         time.sleep(1)
    #         continue

    #     html = result.text
    #     if html:
    #         break

    # 模拟浏览器获取源码, 得到含有文献数据的源码后跳出循环
    driver.get(url)
    for i in range(100):
        time.sleep(1)
        if driver.page_source!=html:
            break

    # 存储
    bson = {}
    html1 = driver.page_source
    bson[‘html‘] = html1
    collection1.insert(bson)

driver.close()

第四步,解析源码

#coding=utf-8
from pymongo import MongoClient
from lxml import etree

client = MongoClient("IP", 27017)
db = client["nstl"]
collection1 = db["journalArticle2017_codes"]
collection2 = db["journalArticle2017_data"]
db.authenticate("","")

zzdw, km, ma, cbn, j, q, qy, zy, zys, flh, gjc, yz, wz = u‘【作者单位】:‘, u‘【刊名】:‘, u‘【ISSN】:‘, u‘【出版年】:‘, u‘【卷】:‘, u‘【期】:‘, u‘【起页】:‘, u‘【止页】:‘, u‘【总页数】:‘, u‘【分类号】:‘, u‘【关键词】:‘, u‘【语种】:‘, u‘【文摘】:‘

# 循环所有论文并构造链接
n = 0
for item in collection1.find({}, {"html":1, "_id":0}):
    html = item["html"]
    tree = etree.HTML(html)

    title = tree.xpath("//span[@name=‘title‘]/text()")
    author = tree.xpath("//a[starts-with(@href,‘javascript:searchByAuthor‘)]/text()")
    organization = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%zzdw)
    journal_name = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%km)
    issn = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%ma)
    publication_year = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%cbn)
    volume = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%j)
    issue = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%q)
    page_start = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%qy)
    page_end = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%zy)
    page_count = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%zys)
    clc = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%flh)
    keywords = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/span/a/text()"%gjc)
    language = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%yz)
    summary = tree.xpath("//div[text()=‘%s‘]/following-sibling::*/text()"%wz)

    dc = {}
    dc[‘title‘] = title[0]
    if author: dc[‘author‘] = author
    if organization: dc[‘organization‘] = organization[0]
    if journal_name: dc[‘journal_name‘] = journal_name[0]
    if issn: dc[‘issn‘] = issn[0]
    if publication_year: dc[‘publication_year‘] = publication_year[0]
    if volume: dc[‘volume‘] = volume[0]
    if issue: dc[‘issue‘] = issue[0]
    if page_start: dc[‘page_start‘] = page_start[0]
    if page_end: dc[‘page_end‘] = page_end[0]
    if page_count: dc[‘page_count‘] = page_count[0]
    if clc: dc[‘clc‘] = clc[0]
    if keywords: dc[‘keywords‘] = keywords[0]
    if language: dc[‘language‘] = language[0]
    if summary: dc[‘summary‘] = summary[0]

    collection2.insert(dc)

原文地址:https://www.cnblogs.com/zhangtianyuan/p/9199324.html

时间: 2024-07-31 04:34:45

Python 爬取外文期刊论文信息(机械 仪表工业)的相关文章

利用python爬取贝壳网租房信息

最近准备换房子,在网站上寻找各种房源信息,看得眼花缭乱,于是想着能否将基本信息汇总起来便于查找,便用python将基本信息爬下来放到excel,这样一来就容易搜索了. 1. 利用lxml中的xpath提取信息 xpath是一门在 xml文档中查找信息的语言,xpath可用来在 xml 文档中对元素和属性进行遍历.对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但xpath明显比re具有优势.具有如下优点:(1)可在xml中查找信息 :(2)支持html的查找:(3)通过元素和属性

python 爬取淘宝模特信息

通过本篇博文,介绍一下我对指定信息进行爬取的时候的思路,顺便贴一下代码. 一.首先获取想要爬取的网站的url链接的规则变化 可以看出来该网站页面的url结构简单,变化的只是https://mm.taobao.com/json/request_top_list.htm?page= page的值 二.对网站页面的DOM树的结构进行分析,方便我们获取我们想要的内容信息, 我写了个简单的网页分析脚本analyze.py:用来输出DOM树,方便我后面做筛选. # -*- coding:utf-8 -*-

python爬取nba今天的信息

最近无聊在写python爬虫,分享一个爬去nba今天信息的python脚本,可能没写的美观,有优化的请大神指点! ?  /test sudo vim nba.py #!/usr/bin/python #-*- coding:utf-8 -*- class url:         def __init__(self,url):                 self.url = url         def nba(self):                 import re      

Python 爬取淘宝商品信息和相应价格

!只用于学习用途! plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html) :获得商品价格和view_price字段,并保存在plt中 tlt = re.findall(r'\"raw_title\"\:\".*?\"',html) :获得商品名称和raw_price字段,并保存在tlt中 price = eval(plt[i].split(':')[1]) :使用冒号分隔键

[Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

一. 文章介绍 前一篇文章"[python爬虫] Selenium爬取新浪微博内容及用户信息"简单讲述了如何爬取新浪微博手机端用户信息和微博信息. 用户信息:包括用户ID.用户名.微博数.粉丝数.关注数等. 微博信息:包括转发或原创.点赞数.转发数.评论数.发布时间.微博内容等. 它主要通过从文本txt中读取用户id,通过"URL+用户ID" 访问个人网站,如柳岩: http://weibo.cn/guangxianliuya 因为手机端数据相对精简简单,所以采用输

[python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息

这篇文章主要对比BeautifulSoup和Selenium爬取豆瓣Top250电影信息,两种方法从本质上都是一样的,都是通过分析网页的DOM树结构进行元素定位,再定向爬取具体的电影信息,通过代码的对比,你可以进一步加深Python爬虫的印象.同时,文章给出了我以前关于爬虫的基础知识介绍,方便新手进行学习.        总之,希望文章对你有所帮助,如果存在不错或者错误的地方,还请海涵~ 一. DOM树结构分析 豆瓣Top250电影网址:https://movie.douban.com/top2

Python 爬取拉勾网python职位信息

今天的任务是爬取拉勾网的职位信息. 首先,我们进入拉勾网,然后在职位搜索栏搜索Python 的同时,打开控制面板F12,来查看网页构成. 在XHR里,可以清楚的看见Ajax请求,所以需要使用session模块来模拟浏览器的行为来操作. 源代码如下: import requests import json header = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Referer': 'https://www.la

Python爬取网页信息

Python爬取网页信息的步骤 以爬取英文名字网站(https://nameberry.com/)中每个名字的评论内容,包括英文名,用户名,评论的时间和评论的内容为例. 1.确认网址 在浏览器中输入初始网址,逐层查找链接,直到找到需要获取的内容. 在打开的界面中,点击鼠标右键,在弹出的对话框中,选择“检查”,则在界面会显示该网页的源代码,在具体内容处点击查找,可以定位到需要查找的内容的源码. 注意:代码显示的方式与浏览器有关,有些浏览器不支持显示源代码功能(360浏览器,谷歌浏览器,火狐浏览器等

Python爬虫项目--爬取自如网房源信息

本次爬取自如网房源信息所用到的知识点: 1. requests get请求 2. lxml解析html 3. Xpath 4. MongoDB存储 正文 1.分析目标站点 1. url: http://hz.ziroom.com/z/nl/z3.html?p=2 的p参数控制分页 2. get请求 2.获取单页源码 1 # -*- coding: utf-8 -*- 2 import requests 3 import time 4 from requests.exceptions import