如何模拟请求中的按钮点击?
How to simulate a button click in a request?
请不要关闭此问题 - 这不是重复问题。我需要使用 Python 请求而不是 Selenium 单击按钮,因为
我正在尝试抓取 Reverso Context translation examples page。我有一个问题:我只能得到 20 个示例,然后我需要点击 "Display more examples" 按钮 很多次,而它存在于页面 上以获得完整的结果列表.它可以简单地使用网络浏览器完成,但我如何使用 Python 请求库来完成?
我查看了按钮的 HTML 代码,但找不到 onclick
属性来查看附加到它的 JS 脚本,我不明白我需要什么请求发送:
<button id="load-more-examples" class="button load-more " data-default-size="14px">Display more examples</button>
这是我的 Python 代码:
from bs4 import BeautifulSoup
import requests
import re
with requests.Session() as session: # Create a Session
# Log in
login_url = 'https://account.reverso.net/login/context.reverso.net/it?utm_source=contextweb&utm_medium=usertopmenu&utm_campaign=login'
session.post(login_url, "Email=reverso.scraping@yahoo.com&Password=sample",
headers={"User-Agent": "Mozilla/5.0", "content-type": "application/x-www-form-urlencoded"})
# Get the HTML
html_text = session.get("https://context.reverso.net/translation/russian-english/cat", headers={"User-Agent": "Mozilla/5.0"}).content
# And scrape it
for word_pair in BeautifulSoup(html_text).find_all("div", id=re.compile("^OPENSUBTITLES")):
print(word_pair.find("div", class_="src ltr").text.strip(), "=", word_pair.find("div", class_="trg ltr").text.strip())
注意: 您需要登录,否则只会显示前10个示例,不会显示按钮。您可以使用此 真实 身份验证数据:
邮箱:reverso.scraping@yahoo.com
密码:样本
这是一个使用 requests
获取所有例句并使用 BeautifulSoup
:
从中删除所有 HTML 标签的解决方案
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)
起初,我从 Google Chrome DevTools 收到请求:
- 按 F12 键进入并选择网络选项卡
- 单击了 "Display more examples" 按钮
- 找到最后一个请求("bst-query-service")
- 右键单击它并选择 复制 > 复制为 cURL (cmd)
然后,我打开 this online-tool,将复制的 cURL 插入左侧的文本框,并复制右侧的输出(为此使用 Ctrl-C 热键,否则可能无法正常工作)。
之后我将它插入 IDE 并且:
- 删除了
cookies
字典 - 此处没有必要
- 重要:将
data
字符串重写为Python字典,并用json.dumps(data)
包裹起来,否则返回一个空词列表的请求。
- 添加了一个脚本,该脚本:多次获取单词 ("pages") 并创建了一个
for
循环来获取单词的次数并在没有 [=63 的情况下打印它们=] 标签(使用 BeautifulSoup)
UPD:
对于那些访问该问题以了解如何使用 Reverso Context(不仅仅是模拟其他网站上的按钮点击请求)的人,有一个 Python Reverso API 的包装器已发布:Reverso-API.它可以做与上面相同的事情,但更简单:
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))
请不要关闭此问题 - 这不是重复问题。我需要使用 Python 请求而不是 Selenium 单击按钮,因为
我正在尝试抓取 Reverso Context translation examples page。我有一个问题:我只能得到 20 个示例,然后我需要点击 "Display more examples" 按钮 很多次,而它存在于页面 上以获得完整的结果列表.它可以简单地使用网络浏览器完成,但我如何使用 Python 请求库来完成?
我查看了按钮的 HTML 代码,但找不到 onclick
属性来查看附加到它的 JS 脚本,我不明白我需要什么请求发送:
<button id="load-more-examples" class="button load-more " data-default-size="14px">Display more examples</button>
这是我的 Python 代码:
from bs4 import BeautifulSoup
import requests
import re
with requests.Session() as session: # Create a Session
# Log in
login_url = 'https://account.reverso.net/login/context.reverso.net/it?utm_source=contextweb&utm_medium=usertopmenu&utm_campaign=login'
session.post(login_url, "Email=reverso.scraping@yahoo.com&Password=sample",
headers={"User-Agent": "Mozilla/5.0", "content-type": "application/x-www-form-urlencoded"})
# Get the HTML
html_text = session.get("https://context.reverso.net/translation/russian-english/cat", headers={"User-Agent": "Mozilla/5.0"}).content
# And scrape it
for word_pair in BeautifulSoup(html_text).find_all("div", id=re.compile("^OPENSUBTITLES")):
print(word_pair.find("div", class_="src ltr").text.strip(), "=", word_pair.find("div", class_="trg ltr").text.strip())
注意: 您需要登录,否则只会显示前10个示例,不会显示按钮。您可以使用此 真实 身份验证数据:
邮箱:reverso.scraping@yahoo.com
密码:样本
这是一个使用 requests
获取所有例句并使用 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)
起初,我从 Google Chrome DevTools 收到请求:
- 按 F12 键进入并选择网络选项卡
- 单击了 "Display more examples" 按钮
- 找到最后一个请求("bst-query-service")
- 右键单击它并选择 复制 > 复制为 cURL (cmd)
然后,我打开 this online-tool,将复制的 cURL 插入左侧的文本框,并复制右侧的输出(为此使用 Ctrl-C 热键,否则可能无法正常工作)。
之后我将它插入 IDE 并且:
- 删除了
cookies
字典 - 此处没有必要 - 重要:将
data
字符串重写为Python字典,并用json.dumps(data)
包裹起来,否则返回一个空词列表的请求。 - 添加了一个脚本,该脚本:多次获取单词 ("pages") 并创建了一个
for
循环来获取单词的次数并在没有 [=63 的情况下打印它们=] 标签(使用 BeautifulSoup)
UPD:
对于那些访问该问题以了解如何使用 Reverso Context(不仅仅是模拟其他网站上的按钮点击请求)的人,有一个 Python Reverso API 的包装器已发布:Reverso-API.它可以做与上面相同的事情,但更简单:
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))