德温特专利数据的爬取(selenium\xpath\contains解决了输入框ID老是动态改变的问题)

(一)目标

针对一系列机构名,获取2016-2021年间的每年申请专利数目以及总数、专利家族数、专利授权量、专利被引频次。

(二)方法

1、使用Webdriver模拟人工访问浏览器

步骤为:
(1)定义检索情况:

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)定位、计算总被引量
从检索结果中定位每个专利的被引量,降序排列后加和所有专利的被引量得到总的被引量
在这里插入图片描述

(3)从筛选器中获取每年的申请量:
在这里插入图片描述
在这里插入图片描述
(3)从筛选器中查看授权/申请状况
在这里插入图片描述

2、BeautifulSoup解析页面

这里,有个坑就是“一定要在点击完页面所有操作的按钮之后再进行解析”,这样会避免后面点击的内容解析不出来的情况。

(三)代码实现

from selenium import webdriver
import time
import json
from pprint import pprint
import requests
import redis
import json
import re
import random
from bs4 import BeautifulSoup
import xlwt
work_book = xlwt.Workbook()

driver = webdriver.Chrome()
driver.get(url='https://derwentinnovation.clarivate.com.cn/login/')
time.sleep(2)
driver.find_element_by_xpath('//*[@id="tr-login-username"]').click()
driver.find_element_by_xpath('//*[@id="tr-login-username"]').clear()
driver.find_element_by_xpath('//*[@id="tr-login-username"]').send_keys('pengh@mail.las.ac.cn')
driver.find_element_by_xpath('//*[@id="tr-login-password"]').click()
driver.find_element_by_xpath('//*[@id="tr-login-password"]').clear()
driver.find_element_by_xpath('//*[@id="tr-login-password"]').send_keys('pengh2018#')
time.sleep(2)
driver.find_element_by_xpath('//*[@id="tr-email-form"]/div/div/div/input').click()
time.sleep(10)

#机构名
lists = [
   ...
]


f = open('result.csv','w',encoding='utf-8')
import  csv
wr_f = csv.writer(f)
for li in lists:
    driver.get(url='https://derwentinnovation.clarivate.com.cn/ui/zh/#/home')
    time.sleep(6)
    '//*[@id="mat-input-61"]'
    driver.find_element_by_xpath('/html/body/di-app/div[1]/div[2]/di-app-home/main/section[1]/div/di-app-smart-search/div/div/di-app-search-type/div[1]/mat-card/a').click()
    time.sleep(6)
    print(li)
    app = 0
    grant = 0
    # start querying
    #first quesy

    driver.find_element_by_xpath('//textarea[contains(@placeholder,"Fanuc")]').click()
    driver.find_element_by_xpath('//textarea[contains(@placeholder,"Fanuc")]').clear()
    driver.find_element_by_xpath('//textarea[contains(@placeholder,"Fanuc")]').send_keys(li)
    time.sleep(5)
    #  query button
    driver.find_element_by_xpath('//button[contains(@class,"but-xl ps-button-3 mat-flat-button mat-button-base mat-primary")]').click()

    time.sleep(10)
    # sort
    try:
        driver.find_element_by_xpath('//*[@id="dataTable_scrollHead_Id"]/div/table/thead/tr/th[12]/div').click()
    except:
        pass
    time.sleep(16)
    # year date
    driver.find_element_by_xpath('//*[@id="filter-sticky-bar"]/div/button').click()
    time.sleep(6)
    try:
        driver.find_elements_by_xpath("//mat-icon[.='arrow_right']")[6].click()
    except:
        wr_f.writerow([li,0,0,0,0,0,0,0,0,0,0,0])
        continue
    time.sleep(10)
    # analyze data
    html = driver.page_source.encode('utf-8')
    soup =BeautifulSoup(html,'lxml')

    #get the zhuanli申请

    content_div = soup.find_all("div",{"class":"mat-radio-label-content"})

    # for div in content_div:
    #     for span in div.find_all('span'):
    #         print(span.getText())
    aa = str(content_div[0].find_all('span')[1].getText()).strip().replace(',','')
    aa = int(aa)
    bb = str(content_div[2].find_all('span')[1].getText()).strip().replace(',','')
    if bb !='':
        bb = int(bb)
    print(aa)
    print(bb)
 

    if aa == '0':
        continue
    else:
        # get the patent_apply for each year
        content1 = soup.find_all("span",{"class":"mat-checkbox-label"})
        temp_2015 = 0
        temp_2016 = 0
        temp_2017 = 0
        temp_2018 = 0
        temp_2019 = 0
        temp_2020 = 0

        for con1 in content1[-14:]:
            time.sleep(2)
            temp = str(con1.getText()).strip()

            if (temp[0] == '2' and 6<len(temp)<=10):
                if temp[0:4] == '2015':
                    temp_2015 += int(temp[5:-1])
                elif temp[0:4] == '2016':
                    temp_2016 += int(temp[5:-1])
                elif temp[0:4] == '2017':
                    temp_2017 += int(temp[5:-1])
                elif temp[0:4] == '2018':
                    temp_2018 += int(temp[5:-1])
                elif temp[0:4] == '2019':
                    temp_2019 += int(temp[5:-1])
                elif temp[0:4] == '2020':
                    temp_2020 += int(temp[5:-1])
            elif temp[0] == 'A':
                app = temp[5:-1]
                print(app)
            elif temp[0] == 'G':
                grant = temp[7:-1]
                print(grant)
        print(temp_2015)
        print(temp_2016)
        print(temp_2017)
        print(temp_2018)
        print(temp_2019)
        print(temp_2020)

        #get the sum citation value
        sum = 0
        content2 = soup.find_all("div",{"class":"dataTables_scrollBody"})[0]
        time.sleep(6)
        for tr in content2.table.tbody.find_all('tr'):
            num = tr.find("td",{"class":"sorting_1"}).getText()
            num = str(num).strip()
            num = int(num)
            sum += num
            # print(num)
            if num == 0:
                break
        print(sum)
    wr_f.writerow([li,aa,bb,temp_2015,temp_2016,temp_2017,temp_2018,temp_2019,temp_2020,app,grant,sum])
f.close()
driver.close()

(四)学习到的点

1、当遇到input or button or不论什么元素的ID老是变动的时候,可以用xpath中的contain方式进行定位!亲测有效、好用!
driver.find_element_by_xpath('//textarea[contains(@placeholder,"Fanuc")]').click()
2、用BeautifulSoup解析页面时,要完成所有的点击操作,使得“style:display=none”的元素变得可见,才能解析出完整的内容!

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
从已经下载好的Derwent专利数据文本提取IPC号也可以采用类似的步骤,具体代码如下: 1. 读取Derwent专利数据文本,提取专利文本。 ``` # 读取Derwent专利数据文本,提取专利文本 import os patent_dir = 'derwent_patent_data' ipc_list = [] for file_name in os.listdir(patent_dir): if file_name.endswith('.txt'): with open(os.path.join(patent_dir, file_name), 'r') as f: content = f.read() # TODO: 提取IPC代码 ``` 2. 使用正则表达式或其他模式匹配算法,从文本提取IPC代码。 ``` # 使用正则表达式从文本提取IPC代码 import re ipc_pattern = r'(?<=IPC:)[^A-Za-z0-9]*([A-H]|[K-N]|[P-T]|[U-Z])[0-9]{2}[A-Z](\d{2}\/\d+)?(?=\s)' for match in re.findall(ipc_pattern, content): ipc_list.append(match[0]) ``` 3. 对提取的IPC代码进行去重和归类。 ``` # 对提取的IPC代码进行去重和归类 unique_ipc_list = list(set(ipc_list)) ipc_dict = {} for ipc_code in unique_ipc_list: ipc_class = ipc_code[0] ipc_subclass = ipc_code[0:4] if ipc_class not in ipc_dict: ipc_dict[ipc_class] = [] ipc_dict[ipc_class].append(ipc_subclass) ``` 4. 分析和可视化提取结果,例如制作IPC代码分布图表。 ``` # 制作IPC代码分布图表 import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() ax.bar(range(len(ipc_dict)), [len(v) for v in ipc_dict.values()]) ax.set_xticks(range(len(ipc_dict))) ax.set_xticklabels(ipc_dict.keys()) ax.set_ylabel('Number of IPC Subclasses') ax.set_xlabel('IPC Class') plt.show() ``` 需要注意的是,以上代码仅是一个示例,具体实现可能需要根据实际数据和任务做出调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值