我正在使用python opengraph库解析网站的opengraph标记https://github.com/erikriver/opengraph。
import opengraph
url = 'http://www.foxnews.com/world/2014/10/20/uk-gun-owners-now-subject-to-warrantless-home-searches/'
og = opengraph.OpenGraph(url=url)
print og.to_json()
当我运行这个脚本时,我得到以下错误
Traceback (most recent call last):
File "test.py", line 16, in <module>
raw = urllib2.urlopen(url)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
urllib2用于在解析html之前获取它https://github.com/erikriver/opengraph/blob/master/opengraph/opengraph.py#L50-L52
为什么我会收到这个404错误?我可以从浏览器访问这个url,还可以使用这个php库https://github.com/scottmac/opengraph检索这个url的open graph标记。
python库能够检索所有其他url的open graph标记,但是这个url似乎是一个异常。
更新时间:
您将得到一个404响应,因为您的请求未通过用户代理。 刚刚在virtualenv上安装了opengraph来测试它,它在header中添加了missing用户代理之后工作:
相关问题 更多 >
编程相关推荐