scholar.py icon indicating copy to clipboard operation
scholar.py copied to clipboard

Use from within Python

Open aidiss opened this issue 9 years ago • 7 comments

What is a good way to use code from within Python? Using package from cmd is a nuisance for me. Thanx for package!

aidiss avatar Dec 13 '14 21:12 aidiss

That's possible: you can import scholar.py as a package (put the file in the same directory as your code or install it as a package) and then copy some of the code from the main() function. An example:

import scholar 

querier = scholar.ScholarQuerier()
settings = scholar.ScholarSettings()
querier.apply_settings(settings)

query = scholar.SearchScholarQuery()
query.set_author("Alan Turing")
query.set_words("computing")
query.set_num_page_results(1)

querier.send_query(query)
# Print the URL of the first article found
print querier.articles[0]['url']

mhkuu avatar Jan 14 '15 15:01 mhkuu

Hi mhkuu, I tried with the line of code you given, But its giving a null array for me(i confirmed with checking length of the array).

sreejithmangattil avatar Nov 30 '16 06:11 sreejithmangattil

Hi @sreejithmangattil, thanks for your reply. The error seems to occur because the current default for the amount of articles returned is 20 (ScholarConf.MAX_PAGE_RESULTS), and for one reason or the other Google Scholar does not accept that. If you explicitly set the number of articles to be returned to another value, the query does return results. I've updated the code above accordingly (added a line with query.set_num_page_results(1)).

mhkuu avatar Nov 30 '16 08:11 mhkuu

Hi mhkuu, thanks a lot for your wonderful response. It works well :+1:

sreejithmangattil avatar Nov 30 '16 10:11 sreejithmangattil

Hi @mhkuu, I am trying to give it just a keyword (set_word) without an author, and then have it return a source with the author, article title, publication, publication date and URL of that source. How do you retrieve all those things, not just the URL, from scholar.py? I tried "print querier.articles[0]['author']" but it just returned None for all the search results I tried.

Example: query.set_words("quantum theory")

Then I want it to print something like: Zinn-Justin J, Quantum field theory and critical phenomena, Oxford : Clarendon Press, 1996, http://cds.cern.ch/record/2280881

max-quirk avatar Dec 17 '17 07:12 max-quirk

Hi @max-quirk, If you look at the ScholarArticle object, you will find that it has no 'author' attribute, so that's why you're getting None as a result. Take a look in the code to see which fields are available. BTW, you do realize you are responding to an issue which had its last activity over a year ago?

mhkuu avatar Dec 19 '17 23:12 mhkuu

@mhkuu Ahh I see. However, the only attributes are 'title', 'url', 'year', 'num_citations', 'num_versions', 'cluster_id', 'url_pdf', 'url_citations', 'url_versions', 'url_citation' and 'excerpt', so how does scholar.py retrieve the authors and publication for the articles?

max-quirk avatar Dec 20 '17 00:12 max-quirk