Abdillah Mohamed
Abdillah Mohamed

Reputation: 11

urllib2.URLError: urlopen error no host given

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.

Upvotes: 1

Views: 2012

Answers (3)

林果皞
林果皞

Reputation: 7793

Note that this error <urlopen error no host given> can occur if you accidentally set empty string '' as proxy in some other place of code, e.g:

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

Upvotes: 0

Rushikesh Badsiwal
Rushikesh Badsiwal

Reputation: 11

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

Upvotes: 1

jophab
jophab

Reputation: 5509

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

Upvotes: 0

Related Questions