如何在请求中模拟按钮单击?

2024-05-14 17:08:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在努力刮。我有一个问题:我只能得到20个示例,然后我需要在页面上多次单击“显示更多示例”按钮以获得完整的结果列表。它可以简单地使用web浏览器来完成,但如何使用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个示例,而不会显示按钮。您可以使用此real身份验证数据:
电子邮件:reverso。scraping@yahoo.com
密码:示例


Tags: textimport示例netsessionmoreloginbutton
1条回答
网友
1楼 · 发布于 2024-05-14 17:08:29

下面是一个解决方案,它使用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开发工具收到了请求:

  1. 按F12键进入并选择网络选项卡
  2. 单击“显示更多示例”按钮
  3. 找到最后一个请求(“bst查询服务”)
  4. 右键单击它并选择“复制”;复制为卷曲(cmd)

然后,我打开this online-tool,将复制的卷曲插入左侧的文本框,并复制右侧的输出(为此使用Ctrl-C热键,否则它可能无法工作)

之后,我将其插入IDE并:

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

UPD:
对于那些访问该问题以了解如何使用Reverso上下文(不仅仅是模拟其他网站上的按钮点击请求)的人来说,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))

相关问题 更多 >

    热门问题