程序化表单提交

发布于 2024-07-10 11:10:53 字数 172 浏览 6 评论 0原文

我想抓取网页的内容。 内容是在该网站上填写并提交表格后生成的。

我已经阅读了如何抓取最终结果内容/网页 - 但如何以编程方式提交表单?

我正在使用 python 并读到我可能需要获取带有表单的原始网页,解析它,获取表单参数,然后执行 X?

有人能指出我正确的方向吗?

I want to scrape the contents of a webpage. The contents are produced after a form on that site has been filled in and submitted.

I've read on how to scrape the end result content/webpage - but how to I programmatically submit the form?

I'm using python and have read that I might need to get the original webpage with the form, parse it, get the form parameters and then do X?

Can anyone point me in the rigth direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-07-17 11:10:53

使用python,我认为需要以下步骤:

  1. 解析包含表单的网页,找出表单提交地址,以及提交方法(“post”或“get”)。

这解释了 html 文件中的表单元素

  1. 使用 urllib2 提交表单。 您可能需要 urllib 中的“urlencode”、“quote”等函数来生成 post 方法的 url 和数据。 阅读库文档了解详细信息。

Using python, I think it takes the following steps:

  1. parse the web page that contains the form, find out the form submit address, and the submit method ("post" or "get").

this explains form elements in html file

  1. Use urllib2 to submit the form. You may need some functions like "urlencode", "quote" from urllib to generate the url and data for post method. Read the library doc for details.
一瞬间的火花 2024-07-17 11:10:53

您需要生成一个包含表单数据的 HTTP 请求。

该表单将类似于:

<form action="submit.php" method="POST"> ... </form>

这告诉您请求的 url 是 www.example.com/submit.php 并且您的请求应该是 POST。

表单中将有几个输入项,例如:

<input type="text" name="itemnumber"> ... </input>

您需要创建一个由所有这些输入名称=值对组成的字符串,该字符串为附加到请求的 URL 末尾的 URL 进行编码,现在变为
www.example.com/submit.php?itemnumber=5234&otherinput=othervalue 等等...
这对于 GET 来说效果很好。 POST 有点棘手。

</motivation>

只需点击 S.Lott 的链接即可获得一些更易于使用的库支持:P

you'll need to generate a HTTP request containing the data for the form.

The form will look something like:

<form action="submit.php" method="POST"> ... </form>

This tells you the url to request is www.example.com/submit.php and your request should be a POST.

In the form will be several input items, eg:

<input type="text" name="itemnumber"> ... </input>

you need to create a string of all these input name=value pairs encoded for a URL appended to the end of your requested URL, which now becomes
www.example.com/submit.php?itemnumber=5234&otherinput=othervalue etc...
This will work fine for GET. POST is a little trickier.

</motivation>

Just follow S.Lott's links for some much easier to use library support :P

别靠近我心 2024-07-17 11:10:53

从类似的问题 - options-for-html-scraping - 你可以通过Python了解到可以使用美丽汤

Beautiful Soup 是一个 Python HTML/XML 解析器,专为屏幕抓取等快速周转项目而设计。 三个功能使其功能强大:

  1. 即使你给它加上不好的标记,《美丽汤》也不会令人窒息。 它生成的解析树与原始文档的意义大致相同。 这通常足以收集您需要的数据并逃跑。
  2. Beautiful Soup 提供了一些简单的方法和 Pythonic 习惯用法,用于导航、搜索和修改解析树:一个用于剖析文档并提取所需内容的工具包。 您不必为每个应用程序创建自定义解析器。
  3. Beautiful Soup 自动将传入文档转换为 Unicode,将传出文档自动转换为 UTF-8。 您不必考虑编码,除非文档未指定编码并且 Beautiful Soup 无法自动检测编码。 然后你只需指定原始编码即可。

这个不寻常的名字引起了我们主持人的注意,2008年11月12日。

From a similar question - options-for-html-scraping - you can learn that with Python you can use Beautiful Soup.

Beautiful Soup is a Python HTML/XML parser designed for quick turnaround projects like screen-scraping. Three features make it powerful:

  1. Beautiful Soup won't choke if you give it bad markup. It yields a parse tree that makes approximately as much sense as your original document. This is usually good enough to collect the data you need and run away.
  2. Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. You don't have to create a custom parser for each application.
  3. Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don't have to think about encodings, unless the document doesn't specify an encoding and Beautiful Soup can't autodetect one. Then you just have to specify the original encoding.

The unusual name caught the attention of our host, November 12, 2008.

夜血缘 2024-07-17 11:10:53

你可以用 JavaScript 来做到这一点。 如果表单类似于:

<form name='myform' ...

那么您可以在 javascript 中执行此操作:

<script language="JavaScript">
function submitform()
{
document.myform.submit();
}
</script> 

您可以使用链接或按钮的“onClick”属性来调用此代码。 要在加载页面时自动调用它,请使用元素的“onLoad”属性:

<body onLoad="submitform()" ...>

You can do it with javascript. If the form is something like:

<form name='myform' ...

Then you can do this in javascript:

<script language="JavaScript">
function submitform()
{
document.myform.submit();
}
</script> 

You can use the "onClick" attribute of links or buttons to invoke this code. To invoke it automatically when a page is loaded, use the "onLoad" attribute of the element:

<body onLoad="submitform()" ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文