【问题标题】:Get form of Just-Eat获取 Just-Eat 的形式
【发布时间】:2024-04-29 23:30:02
【问题描述】:

我想在 Just-Eat 中抓取表格,但表格似乎不存在! 我使用该代码:

br.open("https://www.just-eat.fr/")
form = br.get_forms()

但是表单没有检测到任何表单!但是当您继续查看代码源时,我们可以找到一个表格:

<form class="search-form autocomplete-target" action="#" id="geolocate_form_home">

我不知道如何让它被检测到!有人知道吗?

非常感谢!

【问题讨论】:

  • 这个print(br.parsed) 没有在源代码中显示表单。但它显示带有网址的&lt;iframe&gt;。如果我使用它,它会显示有关阻止机器人/脚本的消息。
  • 如果你运行br.open("https://httpbin.org/get") 然后print(br.parsed) 表明它使用"User-Agent": "python-requests/2.21.0",它可能是主要问题或第一个问题。

标签: python python-3.x web-scraping robobrowser


【解决方案1】:

服务器仅发送带有&lt;iframe&gt; 的页面,其中包含有关出于安全原因进行阻止的消息。

User Agent 标头中的第一个问题。通常 Python 使用python-requests/2.21.0,但服务器可能需要在真实浏览器中使用User Agent。例如 Linux 上的 Firefox

br = robobrowser.RoboBrowser(user_agent='Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0')

但它仍然可以发送带有&lt;iframe&gt; 的页面,其中包含消息。

但如果我再次加载相同的网址,那么它会加载正确的页面。
可能现在它已经拥有了所有需要的 cookie,并且现在服务器不会出现问题。

如果您愿意,您还可以从&lt;iframe&gt; 加载页面以表现得像真人一样。

import robobrowser

br = robobrowser.RoboBrowser(user_agent='Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0')
br.parser = 'lxml'

br.open("https://www.just-eat.fr")
#print(br.parsed)
print(br.get_forms())

#iframe_src = br.select('iframe')[0]['src']
#print(iframe_src)

#br.open("https://www.just-eat.fr"+iframe_src)
#print(br.parsed)

br.open("https://www.just-eat.fr")
#print(br.parsed)
print(br.get_forms())

【讨论】: