python - 是否可以使用 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,但找不到任何东西。
解决方案
一旦您能够访问该网站,您就可以找到这样的例句。
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...', 'А теперь представьте, что у вас есть кот...']
推荐阅读
- python - 熊猫一周中每一天的平均行数
- r - 如何计算zip文件中csv文件的行数
- javascript - 如何操作 `shell.which()` 返回的内容?
- php - 获取模式下动态数组的动态值
- python - 在 QMainWindow 上显示的自定义 QGraphicsView 中的 PyQT 绘图
- ms-access - 将报告拆分为单独的电子邮件及其各自的报告
- git - 如何从失败的 git lfs push 中恢复?
- c# - 不到 10 行前我已经访问过该索引后,索引超出范围
- javascript - 如何在 jQuery 中使用“附加”创建多个 DIV?
- wpf - 当 DependencyProperty 更改时更新另一个属性的值