我在这里尝试做的是获取给定URL的标题,以便我可以确定MIME类型.我希望能够看到是否http://somedomain/foo/会返回HTML文档或JPEG图像.因此,我需要弄清楚如何发送HEAD请求,以便我可以读取MIME类型而无需下载内容.有谁知道这样做的简单方法?
我正在尝试使用Python登录网站并从几个网页收集信息,我收到以下错误:
Run Code Online (Sandbox Code Playgroud)Traceback (most recent call last): File "extract_test.py", line 43, in <module> response=br.open(v) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open return self._mech_open(url, data, timeout=timeout) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open raise response mechanize._response.httperror_seek_wrapper: HTTP Error 429: Unknown Response Code
我用time.sleep()它并且它有效,但它似乎不聪明和不可靠,有没有其他方法来躲避这个错误?
这是我的代码:
import mechanize
import cookielib
import re
first=("example.com/page1")
second=("example.com/page2")
third=("example.com/page3")
fourth=("example.com/page4")
## I have seven URL's I want to open
urls_list=[first,second,third,fourth]
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试提取URL请求的响应头.当我使用firebug分析URL请求的响应输出时,它返回:
Content-Type text/html
Run Code Online (Sandbox Code Playgroud)
但是当我使用python代码时:
urllib2.urlopen(URL).info()
Run Code Online (Sandbox Code Playgroud)
结果输出返回:
Content-Type: video/x-flv
Run Code Online (Sandbox Code Playgroud)
我是python的新手,也是Web编程的新手; 非常感谢任何有用的见解.另外,如果需要更多信息,请告诉我.
提前感谢阅读这篇文章
我需要获取Internet(Intranet)资源的内容类型而不是本地文件.如何从URL后面的资源获取MIME类型:
我试过这个:
res = urllib.urlopen("http://www.iana.org/assignments/language-subtag-registry")
http_message = res.info()
message = http_message.getplist()
Run Code Online (Sandbox Code Playgroud)
我明白了:
['charset=UTF-8']
我怎样才能获得Content-Type,可以使用urllib以及如何以及如果不是这样的方式?
python ×4
http ×2
python-2.7 ×2
content-type ×1
header ×1
http-headers ×1
mechanize ×1
response ×1
url ×1
urllib ×1