urllib2.URLError: urlopen error no host given

徘徊边缘 提交于 2019-12-11 04:55:28

问题


According to this code below, I saved the pull request number in a text file and I want to upload them to the url that is in my code but I got the error mentioned in the title.

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')

Traceback (most recent call last):

  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>

Can someone help me to solve this problem? Thanks.


回答1:


There's an easy fix for that. I took it from https://github.com/rg3/youtube-dl/pull/11892/files (youtube-dl project had this problem too).

Fix in pytube will be like that (I'll try to upload a pull request by today): In api.py file you should change 2 code lines. First, under the function _get_cipher you should change the line:

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

to:

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

Second, under the function from_url you should change this line:

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

to this code:

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



回答2:


The problem is at taking input from file.

read_file.readlines() returns a list, which contains all the lines in the file with a newline character at end of each line.

When you create the url using,

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

Here number will be having a \n at the end.

Therefore the url generated will not be correct.

A better approach is as follows

Read the whole file and split lines using str.splitlines

rf = read_file.read().splitlines()

By using splitlines() you will get the list of lines in the file without \n at the end.

So there will not be the above problem with number



来源:https://stackoverflow.com/questions/41408601/urllib2-urlerror-urlopen-error-no-host-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!