是否可以使用requests模块从Reverso上下文中获取包含单词翻译的示例句子?

2024-05-23 19:47:26 发布

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

我需要从Reverso Context中获得带有单词翻译的示例句子

首先,我尝试获取整个结果页面数据:

import requests


print(requests.get("https://context.reverso.net/translation/english-russian/cat").text)

我在这里遇到了一个问题-服务器知道我正在通过一个机器人访问它:我的应用程序没有得到我需要的东西,除此之外,还得到了其他东西:

<p class="text" id="text-en" style="display: none">
          You've been denied access â IP blacklisted<br/>
          Your IP <b class="ip"></b> has been considered as sending illegitimate traffic to our servers.<br/>
          If you think your traffic is legitimate, please fill in the form below so we could investigate why you were blacklisted.<br/><br/>
          Thank you,<br/>
          The Reverso Team
</p>

有没有办法欺骗服务器并用示例获取页面

注:我试图为这个网站找到一个Python API,但什么也找不到


Tags: textbrip服务器you示例context页面
2条回答

一旦你能够访问网站,你就可以找到这样的例句

req = requests.get("https://context.reverso.net/translation/english-russian/cat", headers={'User-Agent': 'Mozilla/5.0'})

soup = BeautifulSoup(req.text, 'lxml')

sentences = [x.text.strip() for x in soup.find_all('span', {'class':'text'}) if '\n' in x.text]

>>> sentences[:4]
['My cat stepped on the remote.', 'Я не смотрю твои бредни, мой кот наступил на пульт.', 'Now imagine you have a cat...', 'А теперь представьте, что у вас есть кот...']

首先尝试更改请求头中的用户代理,使自己看起来像普通的web浏览器。请参阅https://2.python-requests.org/en/v1.0.4/user/quickstart/#custom-headers,谷歌用户代理标题

相关问题 更多 >