python - urllib2.URL错误: urlopen error no host given

标签 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>

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

最佳答案

有一个简单的解决办法。 我取自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

关于python - urllib2.URL错误: urlopen error no host given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408601/

相关文章:

python - Numpy.arrange 与日期时间 - ValueError : negative dimensions are not allowed:

python - 在 Selenium 中使用 urllib2 cookie

python - 如果 socket.setdefaulttimeout() 不起作用怎么办?

python - 有没有更好的方法来使用 Python 检索网页大小?

Python urllib2 基本认证问题

python - Paramiko 下载、处理并重新上传同一文件

python - 我们如何将 csv/xls 文件中两列的数据读入 2 个变量并将它们用于使用 python 的程序

python - 不重叠的圆检测

python - Pyramid 订户谓词不起作用

python - 调用 urllib.urlopen 时的 Trace/BPT 陷阱