urllib2.URLError:urlopen错误没有给出主机

时间:2016-12-31 13:13:12

标签: python urllib2

根据下面的代码,我将拉取请求编号保存在一个文本文件中,我想将它们上传到我的代码中的网址,但是我收到了标题中提到的错误。

import urllib2
import json
import httplib
def event_spider(org,repo):
    try:
        nbPrequest_reopened=0 #number of pull requests reopened
        pages=1
        while pages<=3:
            headers={'User-Agent':'Mozilla/5.0(X11;Linux i686)',
                'Authorization':'token 516ed78e0521c6b25d9726ad51fa63841d019936',}
            read_file=open('C:\Python27\pullRequest_number.txt','r+')
            rf=read_file.readlines()
            for number in rf:
                url_event=('https://api.github.com/repos/'+ org +'/'+ repo + '/issues/'+ str(number) +'/events?per_page=99&state=all&page='+str(pages))
                event_Request=urllib2.Request(url_event,headers=headers)
                eventObject=urllib2.urlopen(event_Request)
                eventData=json.load(eventObject)
                for element in eventData:
                    if element['event']=='reopened':
                        nbPrequest_reopened+=1
                #print url_event
            pages+=1
    except httplib.BadStatusLine:
        pass
    print 'The number of pull request reopened is %s ' %(nbPrequest_reopened)
if __name__=='__main__':
    event_spider('rails','rails')

追踪(最近一次呼叫最后一次):

  File "C:/Users/ABDILLAH/PycharmProjects/Pandas_data_analysis/event_spider.py", line 27, in <module>
    event_spider('rails','rails')
  File "C:/Users/ABDILLAH/PycharmProjects/Pandas_data_analysis/event_spider.py", line 16, in event_spider
    eventObject=urllib2.urlopen(event_Request)
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 427, in open
    req = meth(req)
  File "C:\Python27\lib\urllib2.py", line 1126, in do_request_
    raise URLError('no host given')
urllib2.URLError: <urlopen error no host given>

有人可以帮我解决这个问题吗? 感谢。

3 个答案:

答案 0 :(得分:1)

有一个简单的解决方法。 我从https://github.com/rg3/youtube-dl/pull/11892/files获取了它(youtube-dl项目也遇到了这个问题)。

在pytube中修复就像那样(我今天会尝试上传一个拉取请求): 在api.py文件中,您应该更改2个代码行。 首先,在函数_get_cipher下,您应该更改行:

reg_exp = re.compile(r'\.sig\|\|([a-zA-Z0-9$]+)\(')

为:

reg_exp = re.compile(r'"signature",\s?([a-zA-Z0-9$]+)\(')

其次,在from_url函数下你应该改变这一行:

js_url = "http:" + video_data.get("assets", {}).get("js")

到此代码:

js_url = '' js_partial_url = video_data.get("assets", {}).get("js")
if js_partial_url.startswith('//'):
    js_url = 'http:' + js_partial_url
elif js_partial_url.startswith('/'):
    js_url = 'https://youtube.com' + js_partial_url

答案 1 :(得分:0)

问题在于从文件中获取输入。

read_file.readlines()返回一个列表,其中包含文件中的所有行,每行末尾都有换行符。

使用时创建网址

url_event=('https://api.github.com/repos/'+ org +'/'+ repo +
 '/issues/'+ str(number) +.....)

此处number最后会有\n

因此生成的网址不正确。

更好的方法如下

使用str.splitlines

读取整个文件并拆分行
rf = read_file.read().splitlines()

使用splitlines(),您将获得文件中的行列表,但最后没有\n

因此number

不会出现上述问题

答案 2 :(得分:0)

请注意,如果您在其他代码位置意外地将空字符串<urlopen error no host given>设置为代理,则会发生此错误''

>>> import urllib2
>>> handlers = []
>>> handlers.append(urllib2.ProxyHandler({'http': '', 'https': ''}))
>>> urllib2.install_opener(urllib2.build_opener(*handlers))
>>> # now in another part of code
... 
>>> urllib2.urlopen('https://stackoverflow.com/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1248, in https_open
    context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1171, in do_open
    raise URLError('no host given')
urllib2.URLError: <urlopen error no host given>
>>>