storerjeremy / python-semrush Goto Github PK
View Code? Open in Web Editor NEWPython wrapper around the SEMrush API.
License: MIT License
Python wrapper around the SEMrush API.
License: MIT License
This repo, uses the requests library to hit the api URL and manage parameters. One issue with this is that when using display_filter, the api expects:
+|Ur|Co|electrical-supplies
(include|URL|Containing|electrical-supplies)
to be:
%2B|Ur|Co|electrical-supplies
if you pass this string to the client.domain_organic method, it will be converted to:
%252B%7CUr%7CCo%7Celectrical-supplies
when Requests builds the URL request.
a workaround is to do the following:
url = "electrical-supplies"
display_filter = quote("+|Ur|Co|" + url, safe='|/', encoding=None, errors=None)
client.api_url = 'http://api.semrush.com/?display_filter='+display_filter
data = client.domain_organic(domain="domain.com", database='us',display_limit=5,export_columns='Ur,Po,Ph,Nq')
The solution would be to simply have the retrieve method of the SemrushClient class handle a kwargs key of 'display_filter' directly on the api URL and let requests handle the remaining params.
There may be some other way to hook into urlencode, or the Requests library to skip encoding '|' characters, but I am not aware of any.
I realize this is a couple-year-old repo, so understood if not wanting to look at. I wanted to leave this here in case someone ran into the same issue.
python-semrush/python_semrush/semrush.py
Line 79 in 4dfd72d
lines = data.splitlines()
python-semrush/python_semrush/semrush.py
Line 67 in 4dfd72d
if 'export_columns' in kwargs:
kwargs['export_columns'] = ",".join([c.strip() for c in kwargs['export_columns'].split(',')])
AttributeError: 'SemrushClient' object has no attribute 'traffic_summary'
There's an issue with parsing returned data with URLs that include a semicolon:
From /python_semrush/semrush.py
84 result = {}
85 for i, datum in enumerate(line.split(';')):
---> 86 result[columns[i]] = datum.strip('"\n\r\t')
87 results.append(result)
As an example this URL was passed from a call to the organic_phrase function:
http://www.hilton.com/en/hotels/content/SPTSHHF/media/pdf/Tangerine_Bar_2.pdf;jsessionid=DTE5TAZBV525MCSGBI12VCQ
Resulting in list index out of range error. To get around this (just in case others find the same problem) I modified my script to declare export_escape=1
in the arguments to force double-quotes; I then updated the parser to split on '";"'
instead of ";"
The new code looks like this:
(lines 75-89 of /python_semrush/semrush.py)
@staticmethod
def parse_response(data):
results = []
data = data.decode('unicode_escape')
lines = data.split('\r\n')
lines = list(filter(bool, lines))
columns = lines[0].split(';')
for line in lines[1:]:
result = {}
for i, datum in enumerate(line.split('";"')):
result[columns[i]] = datum.strip('"\n\r\t')
results.append(result)
return results
I'm sure there is a better way to do this, but in the meantime, this is a workaround that works!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.