You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从Scholarly结果中提取'citedby'后的动态数字并入库

Hey there! Let's break this down step by step since you're new to Python—no worries, we'll get this sorted out smoothly.

Step 1: Extract the Cited-by Number Correctly

First off, let's fix a key issue in your current code: the variable m is a Publication object from the scholarly library, not a raw string. Running re.search directly on m will throw an error because regex works on text, not objects. Here are two solid ways to get that cited-by number:

The Publication object already has a citedby attribute that gives you the exact number—no regex needed! This is way cleaner and less error-prone than parsing strings:

import scholarly

# Fetch the publication object
m = next(scholarly.search_pubs_query('Perception of physical stability and center of mass of 3D objects'))

# Extract the cited-by count directly
cited_by_count = m.citedby
print(cited_by_count)  # Outputs the number like 34567

Method 2: Use Regex (For Learning Purposes)

If you still want to practice regex, convert the Publication object to a string first, then run your pattern:

import scholarly
import re

m = next(scholarly.search_pubs_query('Perception of physical stability and center of mass of 3D objects'))
pub_string = str(m)

# Match "citedby" followed by one or more digits (case-insensitive)
match_result = re.search(r'citedby (\d+)', pub_string, re.IGNORECASE)
if match_result:
    cited_by_count = int(match_result.group(1))
    print(cited_by_count)
else:
    print("No cited-by count found for this publication.")

Step 2: Store the Number in a Database

Let's start with SQLite since it's built into Python—no extra software to install, perfect for beginners. Here's how to set up a table and insert your data:

SQLite Example

import sqlite3

# Connect to a database file (creates it if it doesn't exist)
db_connection = sqlite3.connect('academic_publications.db')
cursor = db_connection.cursor()

# Create a table to store publication data (run this once)
cursor.execute('''
    CREATE TABLE IF NOT EXISTS publications (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT NOT NULL,
        cited_by INTEGER
    )
''')

# Grab the publication title from the Scholarly object
pub_title = m.bib['title']

# Insert the data into the table
cursor.execute('''
    INSERT INTO publications (title, cited_by)
    VALUES (?, ?)
''', (pub_title, cited_by_count))

# Save changes and close the connection
db_connection.commit()
db_connection.close()

print(f"Successfully saved cited-by count {cited_by_count} for: {pub_title}")

For MySQL/PostgreSQL Users

If you need to use a server-based database like MySQL, install the mysql-connector-python package first (pip install mysql-connector-python). The logic is similar—connect to your database, create a table, then insert the data:

import mysql.connector

# Connect to your MySQL database
db_connection = mysql.connector.connect(
    host="your_host_address",
    user="your_username",
    password="your_password",
    database="your_database_name"
)
cursor = db_connection.cursor()

# Create table (run once)
cursor.execute('''
    CREATE TABLE IF NOT EXISTS publications (
        id INT AUTO_INCREMENT PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        cited_by INT
    )
''')

# Insert data
cursor.execute('''
    INSERT INTO publications (title, cited_by)
    VALUES (%s, %s)
''', (pub_title, cited_by_count))

db_connection.commit()
db_connection.close()

A quick pro tip: As you get more comfortable, add try-except blocks to handle edge cases—like if a publication has no cited-by count, or if the database connection fails. It'll make your code more robust!

内容的提问来源于stack exchange,提问作者Teslaturing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:25:57