爬虫 | 【实践】Best Computer Science Scientists数据爬取

2023-10-16 22:52

本文主要是介绍爬虫 | 【实践】Best Computer Science Scientists数据爬取,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 📚数据需求
  • 📚数据爬取
    • 🐇排行榜页数据爬取
    • 🐇获取详情页
    • 🐇目标信息提取
  • 📚完整代码与结果

📚数据需求

  • 姓名,国家,学校
    在这里插入图片描述

  • 最有名研究领域
    在这里插入图片描述

  • 目前研究领域
    在这里插入图片描述

  • 共同作者
    在这里插入图片描述

  • D-index、引用、出版物、世界排名、国家排名
    在这里插入图片描述

📚数据爬取

🐇排行榜页数据爬取

# 以for循环实现翻页,总共20页
for page in range(1, 21):# 前缀f表示该字符串是一个格式化字符串,允许我们在字符串中嵌入变量或表达式的值。# 这里嵌入变量page,实现翻页后的url对应url = f"https://research.com/scientists-rankings/computer-science?page={page}"# 获得响应response = requests.get(url=url, headers=headers)# 智能解码response.encoding = response.apparent_encoding# 使用etree.HTML函数将HTML文本转换为可进行XPath操作的树结构对象tree。tree = etree.HTML(response.text)# 提取id为"rankingItems"元素下的所有div子元素的列表div_list = tree.xpath('//*[@id="rankingItems"]/div')
  • 定位到id="rankingItems
    在这里插入图片描述
  • 每一个div是每一条排行记录
    在这里插入图片描述

🐇获取详情页

# 循环取出div_list内容for i in div_list:# 获取当前科学家的详情页地址href = 'https://research.com' + i.xpath('.//div//h4/a/@href')[0]print(href)# 调用等待时间函数,防止宕机random_wait()# 获得详情页响应response_detail = requests.get(url=href, headers=headers)# 智能解码response.encoding = response.apparent_encoding# 使用etree.HTML函数将HTML文本转换为可进行XPath操作的树结构对象tree。tree_detail = etree.HTML(response_detail.text)
  • .//div//h4/a/@href获取对应科学家详情页相关信息,通过href = 'https://research.com' + i.xpath('.//div//h4/a/@href')[0]得到详情页url
    在这里插入图片描述
  • 对应详情页url如下所示
    在这里插入图片描述

🐇目标信息提取

  • 姓名
    # 名字,依次找到htm → body → 第1个div → 第2个div → 第1个div → div → h1元素,匹配文本内容
    # .strip()用于去除文本内容两端的空白字符,包括空格、制表符和换行符。
    name = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/h1/text()')[0].strip()
    
    在这里插入图片描述

  • 国家

    country = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/div/p/a[2]/text()')[0].strip()
    

    在这里插入图片描述


  • 学校

    university = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/div/p/a[1]/text()')[0].strip()
    

    在这里插入图片描述


  • 最有名研究领域

    try:research_field1 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[0].strip()research_field2 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[1].strip()research_field3 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[2].strip()
    except:# 异常处理,有些详情页无对应数据research_field1="无研究领域"research_field2="无研究领域"research_field3 ="无研究领域"
    

    在这里插入图片描述


  • 目前研究领域

    try:
    # 目前研究领域# 将匹配正则表达式pattern的内容替换为空字符串。删除括号及其内部的内容。now_research_field1 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[0].strip())now_research_field2 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[1].strip())now_research_field3 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[2].strip())
    except:now_research_field1="无研究领域"now_research_field2="无研究领域"now_research_field3 ="无研究领域"
    

    在这里插入图片描述


  • 共同作者
    # 共同作者,定位后源码里的第一个div不要
    Frequent_CoAuthors = tree_detail.xpath('/html/body/div[1]/div[4]/div[2]/div/div')[1:]
    # 共同关系的人
    for i in Frequent_CoAuthors:common_name = i.xpath('.//h4/a/text()')[0].strip().replace('\n', '')friend_list.append(common_name)
    # 将共同关系的人拼成一个字符串
    result = ', '.join(friend_list)
    
    • tree_detail.xpath('/html/body/div[1]/div[4]/div[2]/div/div')[1:]——定位到列表框
      在这里插入图片描述
    • i.xpath('.//h4/a/text()')[0].strip().replace('\n', '')——定位到每个人
      在这里插入图片描述

  • 各项数据、排名等

    # 各项数据,排名等等,[-1:]返回匹配结果列表中的最后一个元素
    data_list = tree_detail.xpath('//*[@id="tab-1"]/div/div')[-1:]
    for a in data_list:# D-indexD_index = a.xpath('.//span[2]//text()')[-1].replace(' ', '').replace('\n', '')# 引用Citations = a.xpath('.//span[3]//text()')[-1].replace(' ', '').replace('\n', '').replace(',', '')# 出版物publication = a.xpath('.//span[4]//text()')[-1].replace(' ', '').replace('\n', '').replace(',', '')# 世界排名world_rank = a.xpath('.//span[5]//text()')[-1].replace(' ', '').replace('\n', '')# 国家排名national_rank = a.xpath('.//span[6]//text()')[-1].replace(' ', '').replace('\n', '')
    
    • //*[@id="tab-1"]/div/div——定位到数据表格
      在这里插入图片描述

    • a.xpath('.//span[2]//text()')[-1]——D-index 在这里插入图片描述

    • a.xpath('.//span[3]//text()')[-1]——引用
      在这里插入图片描述

    • a.xpath('.//span[4]//text()')[-1]——出版物
      在这里插入图片描述

    • 世界排名和国家排名

       # 世界排名world_rank = a.xpath('.//span[5]//text()')[-1].replace(' ', '').replace('\n', '')# 国家排名national_rank = a.xpath('.//span[6]//text()')[-1].replace(' ', '').replace('\n', '')
      

      在这里插入图片描述

      在这里插入图片描述

📚完整代码与结果

import requests
from lxml import etree
import openpyxl
import re
import random
import time# 随机等待时间的函数
# 避免以高频率向服务器发送请求造成宕机
def random_wait():# 生成一个随机的等待时间,范围为1到5秒wait_time = random.uniform(1, 5)time.sleep(wait_time)# openpyxl用于操作Excel文件。它允许我们读取、写入和修改Excel文件中的数据。
# 创建一个新的Excel工作簿对象
workbook = openpyxl.Workbook()
# 返回工作簿中的活动工作表对象,表明之后的代码对这个工作表进行操作
worksheet = workbook.active
# 添加标题
worksheet.append(['姓名', '国家', '学校', '最有名研究领域1', '最有名研究领域2', '最有名研究领域3', '目前研究领域1', '目前研究领域2','目前研究领域3', '共同作者', 'D-index', '引用', '出版物', '世界排名', '国家排名'])# 伪装请求头
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/118.0'
}# 以for循环实现翻页,总共20页
for page in range(1, 21):# 前缀f表示该字符串是一个格式化字符串,允许我们在字符串中嵌入变量或表达式的值。# 这里嵌入变量page,实现翻页后的url对应url = f"https://research.com/scientists-rankings/computer-science?page={page}"# 获得响应response = requests.get(url=url, headers=headers)# 智能解码response.encoding = response.apparent_encoding# 使用etree.HTML函数将HTML文本转换为可进行XPath操作的树结构对象tree。tree = etree.HTML(response.text)# 提取id为"rankingItems"元素下的所有div子元素的列表div_list = tree.xpath('//*[@id="rankingItems"]/div')# 循环取出div_list内容for i in div_list:# 获取当前科学家的详情页地址href = 'https://research.com' + i.xpath('.//div//h4/a/@href')[0]print(href)# 调用等待时间函数,防止宕机random_wait()# 获得详情页响应response_detail = requests.get(url=href, headers=headers)# 智能解码response.encoding = response.apparent_encoding# 使用etree.HTML函数将HTML文本转换为可进行XPath操作的树结构对象tree。tree_detail = etree.HTML(response_detail.text)# 用于删除括号及其内部的内容,主要是对后边最近研究领域后续括号内的百分比进行删除pattern = r'\([^()]*\)'# 存取共同作者的列表friend_list = []try:# 名字,依次找到htm → body → 第1个div → 第2个div → 第1个div → div → h1元素,匹配文本内容# .strip()用于去除文本内容两端的空白字符,包括空格、制表符和换行符。name = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/h1/text()')[0].strip()# 国家country = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/div/p/a[2]/text()')[0].strip()# 学校university = tree_detail.xpath('/html/body/div[1]/div[2]/div[1]/div/div/p/a[1]/text()')[0].strip()# 最有名研究领域try:research_field1 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[0].strip()research_field2 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[1].strip()research_field3 = tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[1]/li/text()')[2].strip()except:# 异常处理,有些详情页无对应数据research_field1="无研究领域"research_field2="无研究领域"research_field3 ="无研究领域"try:# 目前研究领域# 将匹配正则表达式pattern的内容替换为空字符串。删除括号及其内部的内容。now_research_field1 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[0].strip())now_research_field2 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[1].strip())now_research_field3 = re.sub(pattern, '', tree_detail.xpath('//*[@class="tab bg-white shadow"]//ul[4]/li/text()')[2].strip())except:now_research_field1="无研究领域"now_research_field2="无研究领域"now_research_field3 ="无研究领域"# 共同作者,定位后源码里的第一个div不要Frequent_CoAuthors = tree_detail.xpath('/html/body/div[1]/div[4]/div[2]/div/div')[1:]# 共同关系的人for i in Frequent_CoAuthors:common_name = i.xpath('.//h4/a/text()')[0].strip().replace('\n', '')friend_list.append(common_name)# 将共同关系的人拼成一个字符串result = ', '.join(friend_list)# 各项数据,排名等等,[-1:]返回匹配结果列表中的最后一个元素data_list = tree_detail.xpath('//*[@id="tab-1"]/div/div')[-1:]for a in data_list:# D-indexD_index = a.xpath('.//span[2]//text()')[-1].replace(' ', '').replace('\n', '')# 引用Citations = a.xpath('.//span[3]//text()')[-1].replace(' ', '').replace('\n', '').replace(',', '')# 出版物publication = a.xpath('.//span[4]//text()')[-1].replace(' ', '').replace('\n', '').replace(',', '')# 世界排名world_rank = a.xpath('.//span[5]//text()')[-1].replace(' ', '').replace('\n', '')# 国家排名national_rank = a.xpath('.//span[6]//text()')[-1].replace(' ', '').replace('\n', '')print(name, country, university, research_field1, research_field2, research_field3, now_research_field1,now_research_field2, now_research_field3, result, D_index, Citations, publication, world_rank, national_rank)# 清空列表friend_list.clear()# 将数据添加到excel表格内worksheet.append([name, country, university, research_field1, research_field2, research_field3, now_research_field1,now_research_field2, now_research_field3, result, D_index, Citations, publication, world_rank, national_rank])# 保存workbook.save('world_data.csv')except:worksheet.append(['无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据', '无数据'])# 保存workbook.save('world_data.csv')

在这里插入图片描述在这里插入图片描述

这篇关于爬虫 | 【实践】Best Computer Science Scientists数据爬取的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/221299

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired