- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试使用 Python 2 对页面进行 HEAD 请求。
我在努力
import misc_urllib2
.....
opender = urllib2.build_opener([misc_urllib2.MyHTTPRedirectHandler(), misc_urllib2.HeadRequest()])
与 misc_urllib2.py
包含
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def __init__ (self):
self.redirects = []
def http_error_301(self, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_301(
self, req, fp, code, msg, headers)
result.redirect_code = code
return result
http_error_302 = http_error_303 = http_error_307 = http_error_301
但我得到了
TypeError: __init__() takes at least 2 arguments (1 given)
如果我这样做
opender = urllib2.build_opener(misc_urllib2.MyHTTPRedirectHandler())
然后就可以正常使用了
最佳答案
这很好用:
import urllib2
request = urllib2.Request('http://localhost:8080')
request.get_method = lambda : 'HEAD'
response = urllib2.urlopen(request)
print response.info()
使用在 python 中被黑客入侵的快速而肮脏的 HTTPd 进行测试:
Server: BaseHTTP/0.3 Python/2.6.6
Date: Sun, 12 Dec 2010 11:52:33 GMT
Content-type: text/html
X-REQUEST_METHOD: HEAD
我添加了一个自定义标题字段 X-REQUEST_METHOD 以显示它有效:)
这里是 HTTPd 日志:
Sun Dec 12 12:52:28 2010 Server Starts - localhost:8080
localhost.localdomain - - [12/Dec/2010 12:52:33] "HEAD / HTTP/1.1" 200 -
编辑:还有httplib2
import httplib2
h = httplib2.Http()
resp = h.request("http://www.google.com", 'HEAD')
关于python - 从 Python 2 使用 urllib2 发出 HTTP HEAD 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4421170/
如有任何帮助,我将不胜感激。我使用 Python 3.4.1 并尝试导入 urllib.request 和 urllib.parse。没有成功。我总是收到: Traceback (most recen
我正在尝试一个教程代码,它从一个网站获取 html 代码并打印出来。我在 ubuntu 上使用 python 3.4.0。代码: import urllib.request page = urllib
根据这个answer几年前给出了一个相同的问题,Javascript 中的 encodeURIComponent(str) 应该等同于 Python 中的 urllib.quote(str, safe
1。弃用问题 在 Python 3.7 中,我使用 urllib.request.urlretrieve(..) 函数从 URL 下载了一个大文件。在文档 ( https://docs.python.
在 python 3 中,导入时出现此错误:没有名为“urllib.request.urlretrieve”的模块; “urllib.request”不是一个包 import urllib impor
import urllib print urllib.urlopen('http://www.reefgeek.com/equipment/Controllers_&_Monitors/Neptune
我在 gooogle colab 中使用来自 parselmouth 的 praat,在导入 from parselmouth.praat import call 时出现此错误 /usr/local/
是否有与 Python 的 urllib.parse.quote() 等效的 JavaScript 函数?和 urllib.parse.unquote() ? 我遇到的最接近的是encodeURI()
这个问题在这里已经有了答案: Importing installed package from script with the same name raises "AttributeError: m
Python 的 urllib.quote 和 urllib.unquote 在 Python 2.6.5 中无法正确处理 Unicode。这就是发生的事情: In [5]: print urllib
这个问题在这里已经有了答案: How to route urllib requests through the TOR network? [duplicate] (3 个回答) 关闭6年前。 示例代码
我正在制作一些简单的 python 帖子脚本,但效果不佳。 有两部分必须登录。 第一次登录使用' http://mybuddy.buddybuddy.co.kr/userinfo/UserInfo.a
我有以下脚本: from currency_converter import CurrencyConverter test = CurrencyConverter('http://www.ecb.eu
我正在编写一个小工具来监控学校的开课情况。 我编写了一个 python 脚本,每隔几分钟就会从每个部门获取当前类(class)的可用性。 该脚本一直正常运行,直到大学网站开始返回以下内容: SIS S
为什么下面的结果会出错? import re from urllib import quote as q s = re.compile(r'[^a-zA-Z0-9.: ^*$@!+_?-]') s.s
我正在开发一个网络爬虫来自动下载巴西网站上的一些文档。并且它使用了一些未知的编码(head 标签中没有定义字符集)。 人们只需付出很少的努力就可以阅读这些文档。但真正的问题是,列出文档的页面使用的链接
我有一个程序,我需要打开许多网页并下载其中的信息。然而,这些信息位于页面中间,需要很长时间才能找到。有没有办法让 urllib 只检索 x 行?或者,如果没有别的事,之后就不加载信息? 我在 Mac
我有一个脚本,使用 Urllib 打开我安装了谷歌分析的网页。我的问题是,为什么如果我执行脚本,GA 上不会显示访问次数? 最佳答案 Google Analytics 脚本是 JavaScript 代
我正在尝试下载航类搜索结果,但我不断收到一个与通过右键单击并手动存储网站获得的文件不同的文件。我已经尝试过 urllib 、 urllib2 以及我在这里找到的每个命令都无济于事。 这是一个 MWE:
我最近用Python(Windows 64位v3.3.1)编写了一个程序,并试图将其移植到D。我遇到的问题是我使用了urllib Python 中的模块,特别是 urllib.request.Requ
我是一名优秀的程序员,十分优秀!