Skip to content

Added new parameter lr to the YandexXML class constructor. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pyyaxml/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
API_USER = ''
API_KEY = ''

y = YaSearch(API_USER, API_KEY)
# Put the desired region https://yandex.ru/dev/xml/doc/dg/reference/regions.html
LR_S = '213'

y = YaSearch(API_USER, API_KEY, LR_S)
results = y.search('питон', site='python.su', page=1)
if results.error is None:
for result in results.items:
print(result.url + ' ' + result.snippet)
else:
print(results.error.code + ' ' + results.error.description)

9 changes: 5 additions & 4 deletions pyyaxml/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class YaSearch:
<sortby order="%s" priority="no">%s</sortby>
<maxpassages>0</maxpassages>
<groupings>
<groupby mode="flat"/>
</groupings>
<groupby mode="flat"/>
</groupings>
</request>"""

BASE_URL = u'https://yandex.{}/search/xml?'
Expand All @@ -48,9 +48,10 @@ def _xml_extract_helper(self, node):
result += self._xml_extract_helper(child)
return result.strip()

def __init__(self, api_user, api_key, domain='ru'):
def __init__(self, api_user, api_key, lr=None, domain='ru'):
self._api_user = api_user
self._api_key = api_key
self._lr = lr
if domain not in self.VALID_DOMAINS:
raise ValueError('Invalid domain. Valid domains are {}'.format(', '.join(self.VALID_DOMAINS)))
self._url = self.BASE_URL.format(domain)
Expand Down Expand Up @@ -102,7 +103,7 @@ def search(self, query, page=1, site=None, max_page_num=100, sort_by='rlv', orde
request_suffix += (u' site:%s' % site)

page -= 1
params = {'user': self._api_user, 'key': self._api_key}
params = {'user': self._api_user, 'key': self._api_key, 'lr': self._lr}
query = query + request_suffix

if PY2:
Expand Down