草庐IT

Python - 使用 urllib2 opener 发布

我有一个urllib2opener,并希望将它用于带有一些数据的POST请求。我希望收到我要发布到的页面的内容,以及返回的页面的URL(我认为这只是一个30x代码;所以按照这些行的东西会很棒!)。将此视为代码:anOpener=urllib2.build_opener(???,???)anOpener.addheaders=[(???,???),(???,???),...,(???,???)]#dosomeotherstuffwiththeopenerdata=urllib.urlencode(dictionaryWithPostValues)pageContent=anOpener.

Python:单击带有 urllib 或 urllib2 的按钮

我想用python点击一个按钮,表单的信息由网页自动填充。向按钮发送请求的HTML代码是:INPUTtype="submit"value="PlaceaBid">我该怎么做呢?是否可以仅使用urllib或urllib2单击按钮?还是我需要使用Mechanize或斜纹布之类的东西? 最佳答案 使用表单目标并将任何输入作为发布数据发送,如下所示:..................python:#parsethepageHTMLwiththeformtogettheformtargetandanyinputnamesandvalues.

python - 为什么我在使用 urllib2 请求 URL 时得到 “HTTP Error 405: Method Not Allowed”?

我在python中使用urllib2和urllib库假设我有以下代码importurllib2importurlliburl='http://ah.example.com'half_url=u'/servlet/av/jd?ai=782&ji=2624743&sn=I'req=urllib2.Request(url,half_url.encode('utf-8'))response=urllib2.urlopen(req)printresponse当我运行上面的代码时,出现以下错误Traceback(mostrecentcalllast):File"example.py",line39

python - urllib2 HTTP 错误 429

所以我有一个子reddits列表,我正在使用urllib打开它们。当我浏览它们时,最终urllib失败了:urllib2.HTTPError:HTTPError429:Unknown做一些研究我发现reddit通过IP限制了对其服务器的请求数量:Makenomorethanonerequesteverytwoseconds.There'ssomeallowanceforburstsofrequests,butkeepitsane.Ingeneral,keepittonomorethan30requestsinaminute.所以我想我会使用time.sleep()将我的请求限制为每10

python - urllib.request 模块无法安装到我的系统中

尝试使用以下命令安装urllib.request模块sudopipinstallurllib.request但它回来了Downloading/unpackingurllib.requestCouldnotfindanydownloadsthatsatisfytherequirementurllib.requestCleaningup...Nodistributionsatallfoundforurllib.requestStoringdebuglogforfailurein/home/mounarajan/.pip/pip.log如何安装这个模块? 最佳答案

Python urllib urlopen 不工作

我只是想通过使用urllib模块从实时网络中获取数据,所以我写了一个简单的例子这是我的代码:importurllibsock=urllib.request.urlopen("http://diveintopython.org/")htmlSource=sock.read()sock.close()print(htmlSource)但是我得到了这样的错误:Traceback(mostrecentcalllast):File"D:\test.py",line3,insock=urllib.request.urlopen("http://diveintopython.org/")Attrib

python - 我正在使用 Python urllib2 下载文件。如何检查文件大小?

如果它很大...那么停止下载?我不想下载大于12MB的文件。request=urllib2.Request(ep_url)request.add_header('User-Agent',random.choice(agents))thefile=urllib2.urlopen(request).read() 最佳答案 没有必要bobince做了并放到了httplib。您可以直接使用urllib完成所有这些操作:>>>importurllib2>>>f=urllib2.urlopen("http://dalkescientific.c

python - 使用 python urllib2 发送 POST 请求并获得响应

我正在尝试通过发送POST请求取回HTML页面:importhttplibimporturllibimporturllib2fromBeautifulSoupimportBeautifulSoupheaders={'Host':'digitalvita.pitt.edu','Connection':'keep-alive','Content-Length':'325','Origin':'https://digitalvita.pitt.edu','User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_7_4)AppleWebKit/537.

python - 使用 urllib2+httplib.debuglevel 调试连​​接有时不显示调试信息

试图让登录脚本正常工作,我一直返回相同的登录页面,所以我打开了http流的调试(由于https,不能使用wireshark等)。我什么都没有,所以我复制了这个例子,它有效。对google.com的任何查询都有效,但对我的目标页面不显示调试,有什么区别?如果它是重定向,我希望看到第一个get/redirectheader和http://google也重定向。importurllibimporturllib2importpdbh=urllib2.HTTPHandler(debuglevel=1)opener=urllib2.build_opener(h)urllib2.install_op

python - 为不同的响应模拟 urllib2.urlopen().read()

我正在尝试以某种方式模拟urllib2.urlopen库,以便我应该对传递给函数的不同url获得不同的响应。我现在在我的测试文件中的做法是这样的@patch(othermodule.urllib2.urlopen)defmytest(self,mock_of_urllib2_urllopen):a=Mock()a.read.side_effect=["response1","response2"]mock_of_urllib2_urlopen.return_value=aothermodule.function_to_be_tested()#thisisthefunctionwhich