ALEXA1003 2024-12-01 12:33 采纳率: 0%
浏览 51
已结题

学术论文作者工作经历和教育经历爬虫

爬虫问题,不论方法希望爬取到作者工作信息和教育信息

已有数据是一个Excel表格,版面如图所示

img

其中,作者栏是一纵列,包括第一作者第二作者等,如何爬取这些信息呢?由于每个作者在其工作单位的个人介绍页面网址端口并不相同,国内也没有统一的学术作者介绍网站,故本人无法编译出统一的代码去定向爬取。国内ChatGPT等人工智能大语言模型因为通过节点连接,无法联网搜索,故也无法帮助本人搜索并导出这些作者的教育和工作信息。可以从百度百科/高校界面/维基百科等任何可信网站获得信息,能爬到即可,不要求是python,八爪鱼也行

有一段代码,但不一定有用,可不用,只要结果,私信获得所有Excel数据

import pandas as pd
import requests
from bs4 import BeautifulSoup
import re
import time
import logging
 
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
input_file = 'E:/资料/金融/RA/Python源代码/1234.xlsx'
df = pd.read_excel(input_file)
 
bing_search_url = "https://www.bing.com/search?q={}"
results = []
 
for index, row in df.iterrows():
    authors = row['作者']
    institution = row['作者单位']
 
    author_names = re.split(r'[,\s;;]+', authors)
    author_names = [re.sub(r'\d+$', '', name).strip() for name in author_names if name]
 
    for author_name in author_names:
        try:
            search_query = f"{author_name} {institution} 教育背景 工作经历"
            response = requests.get(bing_search_url.format(search_query), headers={"User-Agent": "Mozilla/5.0"})
            response.raise_for_status()
 
            soup = BeautifulSoup(response.content, 'html.parser')
            found_info = False
 
            for i, result in enumerate(soup.find_all('li', {'class': 'b_algo'}), start=1):
                title = result.find('h2')
                snippet = result.find('p')
 
                if title and snippet:
                    info_text = snippet.get_text().strip()
                    results.append({
                        '作者': author_name,
                        '作者单位': institution,
                        '教育背景与工作经历': info_text
                    })
                    found_info = True
                    logging.info(f'已成功爬取 {author_name} 的信息:{info_text}')
                    break
                if i > 5:
                    break
 
            if not found_info:
                logging.warning(f'未找到 {author_name} 的相关信息')
                results.append({
                    '作者': author_name,
                    '作者单位': institution,
                    '教育背景与工作经历': '未找到'
                })
 
            time.sleep(2)
        
        except requests.exceptions.HTTPError as e:
            logging.error(f'HTTP错误: {e}')
            results.append({
                '作者': author_name,
                '作者单位': institution,
                '教育背景与工作经历': '未找到'
            })
        except Exception as e:
            logging.error(f'爬取 {author_name} 时出错: {e}')
            results.append({
                '作者': author_name,
                '作者单位': institution,
                '教育背景与工作经历': '出错'
            })
 
output_df = pd.DataFrame(results)
output_file = 'E:/资料/金融/RA/Python源代码/authors_bing_education_work_experience.xlsx'
output_df.to_excel(output_file, index=False)
logging.info(f'所有数据已保存至 {output_file}')


  • 写回答

34条回答 默认 最新

  • 阿里嘎多学长 2024-12-01 12:58
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    答案

    🤔 首先,让我们分解这个问题。您想要爬取作者的工作经历和教育经历信息,但是这些信息并不是一个整体,可以通过分析作者个人介绍页来爬取。

    1. 数据预处理:首先,您需要将Excel表格中的作者信息转换为 Python 可读的格式,这可能涉及到 JSON、CSV 或 pickle 等格式的转换。
    2. 作者个人介绍页爬取:然后,您可以使用 Python 中的爬虫库,例如 requestsBeautifulSoup,爬取作者个人介绍页中的工作经历和教育经历信息。
    3. 信息提取:在爬取的页面中,您可以使用 regular expression 或 BeautifulSoup 的selectors来提取特定的信息,例如工作年份、工作单位、学历、专业等。

    以下是一些可能会有用的 Python 代码:

    import requests
    from bs4 import BeautifulSoup
    import json
    
    # 数据预处理
    authors = json.load(open('authors.json'))  # 从Excel表格中读取作者信息
    
    # 作者个人介绍页爬取
    for author in authors:
        url = f"https://example.com/{author['name']}"  # author的个人介绍页 URL
        response = requests.get(url)
        soup = BeautifulSoup(response.content, 'html.parser')
    
        # 信息提取
        work_experience = []
        education = []
        for item in soup.find_all('li'):
            if item.text.startswith('工作经历'):
                work_experience.append(item.text.strip())
            elif item.text.startswith('教育'):
                education.append(item.text.strip())
    
        # 数据存储
        author_info = {'name': author['name'], 'work_experience': work_experience, 'education': education}
        authors_data.append(author_info)
    
    # 将爬取的数据存储到 Excel 表格中
    with open('authors_data.json', 'w') as f:
        json.dump(authors_data, f, indent=4)
    

    请注意,这只是一个简单的示例代码,您可能需要根据实际情况进行修改和调整。

    另外,如果您使用 ChatGPT 可以帮助您完成爬取和信息提取工作,但是您需要提供实际的个人介绍页 URL 和相关信息,以便ChatGPT正确地爬取和分析信息。

    评论

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 创建了问题 12月1日