【发布时间】:2016-05-15 17:32:20
【问题描述】:
我大致了解如何使用 urllib2 发出POST 请求(编码数据等),但问题是所有在线教程都使用完全无用的虚构示例 url 来展示如何做到这一点(@ 987654327@、coolsite.org 等),所以我看不到与他们使用的示例代码对应的特定 html。就连python.org自己的tutorial在这方面也完全没用。
我需要向这个网址发出POST 请求:
https://patentscope.wipo.int/search/en/search.jsf
代码的相关部分是这样的(我认为):
<form id="simpleSearchSearchForm" name="simpleSearchSearchForm" method="post" action="/search/en/search.jsf" enctype="application/x-www-form-urlencoded" style="display:inline">
<input type="hidden" name="simpleSearchSearchForm" value="simpleSearchSearchForm" />
<div class="rf-p " id="simpleSearchSearchForm:sSearchPanel" style="text-align:left;z-index:-1;"><div class="rf-p-hdr " id="simpleSearchSearchForm:sSearchPanel_header">
或者可能是这样的:
<input id="simpleSearchSearchForm:fpSearch" type="text" name="simpleSearchSearchForm:fpSearch" class="formInput" dir="ltr" style="width: 400px; height: 15px; text-align: left; background-image: url("https://patentscope.wipo.int/search/org.richfaces.resources/javax.faces.resource/org.richfaces.staticResource/4.5.5.Final/PackedCompressed/classic/org.richfaces.images/inputBackgroundImage.png"); background-position: 1px 1px; background-repeat: no-repeat;">
如果我想将JP2014084003编码为搜索词,html中对应的值是什么? input id? name? value?
附录:this answer 没有回答我的问题,因为它只是重复了我已经在 python 文档页面中查看过的信息。
更新:
我找到了this,并试了一下里面的代码,具体来说:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'name':'simpleSearchSearchForm:fpSearch','value':'2014084003'}
link = 'https://patentscope.wipo.int/search/en/search.jsf'
session = requests.Session()
resp = session.get(link,headers=headers)
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
resp = session.post(link,headers=headers,data=payload,cookies =cookies)
r = session.get(link)
f = open('htmltext.txt','w')
f.write(r.content)
f.close()
我收到了成功的回复 (200),但数据再次只是原始页面中的数据,所以我不知道我是否正确地发布到表单,还有其他我需要做的事情做让它从搜索结果页面返回数据,或者如果我仍然发布错误的数据。
是的,我意识到这使用requests 而不是urllib2,但我想做的就是获取数据。
【问题讨论】:
-
搜索是作为表单数据发送的,而不是作为 URL 的一部分。查询文本的键是
simpleSearchSearchForm:fpSearch。请注意,当您手动提交搜索时,您可以使用浏览器中的开发者工具看到这一点。 -
好的。那是什么意思?我的意思是:我应该怎么做才能发布数据并检索我需要的页面?
-
我阅读了该页面,但它没有包含我需要的信息。
-
然后前往谷歌并找到可以做到的地方 - 在您显然已经阅读过的教程中,请参阅docs.python.org/2/howto/urllib2.html#data
标签: python python-2.7 post python-requests urllib2