-
Notifications
You must be signed in to change notification settings - Fork 7
Adaptation of sparql_dataframe to Wikidata #10
Comments
Hello, Try passing
You can see in the unit tests that queries against Wikidata should work fine with |
HTTPError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\sparql_dataframe\sparql_dataframe.py in get_sparql_dataframe(endpoint, query, post) C:\ProgramData\Anaconda3\lib\site-packages\SPARQLWrapper\Wrapper.py in query(self) C:\ProgramData\Anaconda3\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self) C:\ProgramData\Anaconda3\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self) C:\ProgramData\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) C:\ProgramData\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout) C:\ProgramData\Anaconda3\lib\urllib\request.py in http_response(self, request, response) C:\ProgramData\Anaconda3\lib\urllib\request.py in error(self, proto, *args) C:\ProgramData\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args) C:\ProgramData\Anaconda3\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs) HTTPError: HTTP Error 403: Forbidden |
I think that's an error returned by the actual Wikidata SPARQL endpoint. It aggressively rate limits. |
How would you read the query saved into a separate file? Thanks for your help ! |
If your queries are saved in a text file, then you would just read them in like any other text file in Python and save them to a Here's a tutorial on reading and writing files in Python: https://realpython.com/read-write-files-python/#reading-and-writing-opened-files |
This works :
|
Just had the same issue issue querying wikidata. First thought, it might be caused by a version change (SPARQLWrapper was installed in version 2.0.0). It now already contains Nevertheless, thanks for creating this lib which made it directly into the wrapper!
|
Hello,
I am trying to extract dataframes from queries in Wikidata.
For instance, this code from an example in Wikidata works to extract dictionary of countries:
`# pip install sparqlwrapper # https://rdflib.github.io/sparqlwrapper/
import sparql_dataframe
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
endpoint_url = "https://query.wikidata.org/sparql"
query = """#Countries
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q6256.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}"""
def get_results(endpoint_url, query):
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
results = get_results(endpoint_url, query)
for result in results["results"]["bindings"]:
print(result)
`
When I do that :
df = sparql_dataframe.get(endpoint_url, query)
I receive this error:
C:\ProgramData\Anaconda3\lib\site-packages\SPARQLWrapper\Wrapper.py:1315: RuntimeWarning: Format requested was CSV, but XML (application/sparql-results+xml;charset=utf-8) has been returned by the endpoint
warnings.warn(message % (requested.upper(), format_name, mime), RuntimeWarning)
AttributeError Traceback (most recent call last)
in
----> 1 df = sparql_dataframe.get(endpoint_url, query)
C:\ProgramData\Anaconda3\lib\site-packages\sparql_dataframe\sparql_dataframe.py in get_sparql_dataframe(endpoint, query, post)
28 sparql.setReturnFormat(CSV)
29 results = sparql.query().convert()
---> 30 _csv = StringIO(results.decode('utf-8'))
31 return pd.read_csv(_csv, sep=",")
AttributeError: 'Document' object has no attribute 'decode'
The text was updated successfully, but these errors were encountered: