Skip to content

Instantly share code, notes, and snippets.

@ShaikeA
Last active November 15, 2019 12:57
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ShaikeA/81e3320cbf542cf246601c8a9356be71 to your computer and use it in GitHub Desktop.
def update_num_citations(my_db, cursor):
# Get all articles from DB (doi_link is a unique key)
sql_get_articles = """SELECT doi_link, citations, last_name FROM authors;"""
cursor.execute(sql_get_articles)
articles = cursor.fetchall()
# Get the updated number of citations (Using requests and BeautifulSoup, open each URL of each doi_link and extract the number of citations)
articles_dict = [{'doi_link': articles[ind][0], 'citations': articles[ind][1]} for ind in range(len(authors_no_gen))]
articles_dict = get_citations(articles_dict)
# Update the data in the DB
for doi_link, citations in articles_dict:
sql_update = """ UPDATE articles SET citations = {0} WHERE (doi_link = '{1}');""".format(int(citations), doi_link)
cursor.execute(sql_update)
# The for loop can be replaced with:
# cur.executemany("UPDATE articles SET citations = {0} WHERE (doi_link = '{1}');".format(int(citations), doi_link),[--tuples of the data to be updated--])
my_db.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment