首页 > 解决方案 > 如何模拟请求中的按钮单击?

问题描述

请不要关闭这个问题 - 这不是重复的。我需要使用 Python 请求而不是 Selenium 来单击按钮,如此

我正在尝试抓取Reverso Context 翻译示例页面。我有一个问题:我只能获得 20 个示例,然后我需要在页面上存在“显示更多示例”按钮时多次单击它才能获得完整的结果列表。它可以简单地使用 Web 浏览器完成,但是如何使用 Python Requests 库来完成呢?

我查看了按钮的 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 个示例,并且不会显示按钮。您可以使用这个真实的身份验证数据:
E-mail: reverso.scraping@yahoo.com
密码: sample

标签: pythonweb-scrapingbeautifulsouppython-requestsurllib

解决方案


这是一个解决方案,它使用以下方式获取所有例句requests并从中删除所有 HTML 标记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 的请求:

  1. F12键进入并选择网络选项卡
  2. 单击“显示更多示例”按钮
  3. 找到最后一个请求(“bst-query-service”)
  4. 右键单击它并选择复制 > 复制为 cURL (cmd)

然后,我打开了这个在线工具,将复制的 cURL 插入到左侧的文本框中,并复制了右侧的输出(为此使用Ctrl-C热键,否则可能不起作用)。

之后,我将其插入 IDE 并:

  1. 删除了cookiesdict - 这里没有必要
  2. 重要提示:将data字符串重写为 Python 字典并用 包裹起来json.dumps(data),否则,它返回一个带有空单词列表的请求。
  3. 添加了一个脚本,该脚本:获取多次获取单词(“页面”)并创建一个for循环,获取该次数的单词并在没有 HTML 标记的情况下打印它们(使用 BeautifulSoup)

UPD:
对于那些访问该问题以了解如何使用Reverso Context(不仅仅是模拟其他网站上的按钮点击请求)的人来说,有一个针对Reverso API 的Python 包装器已发布: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))

推荐阅读