I need to translate Russian text to text I can put in a link. For example, I want to find news about Russian company "Сбербанк". I need to use this link to parse data: https://news.google.com/rss/search?q=%D1%81%D0%B1%D0%B5%D1%80%D0%B1%D0%B0%D0%BD%D0%BA&hl=ru&gl=RU&ceid=RU:ru, where %D1%81%D0%B1%D0%B5%D1%80%D0%B1%D0%B0%D0%BD%D0 means "Сбербанк". How can I translate the words like "Сбербанк" into the format, that I can put in the link?
Asked
Active
Viewed 271 times
1 Answers
1
If you just want to encode that part, you can use urllib.parse.quote_plus:
import urllib.parse
data = "сбербанк"
print(urllib.parse.quote_plus(data))
# %D1%81%D0%B1%D0%B5%D1%80%D0%B1%D0%B0%D0%BD%D0%BA
Or you could simply use the requests library, which is recommended by the official documentation. It is easier to use and will take care of that for you:
import requests
url = "https://news.google.com/rss/search"
payload = {'q': "сбербанк",
'hl': 'ru',
'gl': 'RU',
'ceid': 'RU:ru'
}
req = requests.get(url, params=payload)
print(req.url)
# https://news.google.com/rss/search?q=%D1%81%D0%B1%D0%B5%D1%80%D0%B1%D0%B0%D0%BD%D0%BA&hl=ru&gl=RU&ceid=RU%3Aru
print(req.text)
# <?xml version="1.0" encoding="UTF-8" standalone="yes"?><rss version="2.0" ......

Thierry Lathuille
- 23,663
- 10
- 44
- 50