Please do not close this question - this is not a duplicate. I need to click the button using Python requests, not Selenium, as here
I am trying to
Here is a solution that gets all the example sentences using requests
and removes all the HTML tags from them using BeautifulSoup
:
from bs4 import BeautifulSoup
import requests
import json
headers = {
"Connection": "keep-alive",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": "96",
"Origin": "https://context.reverso.net",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Referer": "https://context.reverso.net/^%^D0^%^BF^%^D0^%^B5^%^D1^%^80^%^D0^%^B5^%^D0^%^B2^%^D0^%^BE^%^D0^%^B4/^%^D0^%^B0^%^D0^%^BD^%^D0^%^B3^%^D0^%^BB^%^D0^%^B8^%^D0^%^B9^%^D1^%^81^%^D0^%^BA^%^D0^%^B8^%^D0^%^B9-^%^D1^%^80^%^D1^%^83^%^D1^%^81^%^D1^%^81^%^D0^%^BA^%^D0^%^B8^%^D0^%^B9/cat",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
}
data = {
"source_text": "cat",
"target_text": "",
"source_lang": "en",
"target_lang": "ru",
"npage": 1,
"mode": 0
}
npages = requests.post("https://context.reverso.net/bst-query-service", headers=headers, data=json.dumps(data)).json()["npages"]
for npage in range(1, npages + 1):
data["npage"] = npage
page = requests.post("https://context.reverso.net/bst-query-service", headers=headers, data=json.dumps(data)).json()["list"]
for word in page:
print(BeautifulSoup(word["s_text"]).text, "=", BeautifulSoup(word["t_text"]).text)
At first, I got the request from the Google Chrome DevTools:
Then, I opened this online-tool, insert the copied cURL to the textbox on the left and copied the output on the right (use Ctrl-C hotkey for this, otherwise it may not work).
After that I inserted it to the IDE and:
cookies
dict - it is not necessary heredata
string as a Python dictionary and wrapped it with json.dumps(data)
, otherwise, it returned a request with empty words list.for
loop that gets words this number of times and prints them without HTML tags (using BeautifulSoup)UPD:
For those, who visited the question to learn how to work with Reverso Context (not just to simulate a button click request on other website) there is a Python wrapper for Reverso API released: Reverso-API. It can do the same thing as above but much simpler:
from reverso_api.context import ReversoContextAPI
api = ReversoContextAPI("cat", "", "en", "ru")
for source, target in api.get_examples_pair_by_pair():
print(highlight_example(source.text), "==", highlight_example(target.text))