首页 > 解决方案 > 是否可以使用 requests 模块从 Reverso Context 获取带有单词翻译的例句?

问题描述

我需要从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>

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

PS:我试图为这个网站找到一个 Python API,但找不到任何东西。

标签: pythonweb-scrapingpython-requestsurllib

解决方案


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

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...', 'А теперь представьте, что у вас есть кот...']

推荐阅读