How to handle IncompleteRead: in python

后端 未结 8 1663
面向向阳花
面向向阳花 2020-12-05 10:12

I am trying to fetch some data from a website. However it returns me incomplete read. The data I am trying to get is a huge set of nested links. I did some rese

8条回答
  •  盖世英雄少女心
    2020-12-05 10:43

    You can use requests instead of urllib2. requests is based on urllib3 so it rarely have any problem. Put it in a loop to try it 3 times, and it will be much stronger. You can use it this way:

    import requests      
    
    msg = None   
    for i in [1,2,3]:        
        try:  
            r = requests.get(self.crawling, timeout=30)
            msg = r.text
            if msg: break
        except Exception as e:
            sys.stderr.write('Got error when requesting URL "' + self.crawling + '": ' + str(e) + '\n')
            if i == 3 :
                sys.stderr.write('{0.filename}@{0.lineno}: Failed requesting from URL "{1}" ==> {2}\n'.                       format(inspect.getframeinfo(inspect.currentframe()), self.crawling, e))
                raise e
            time.sleep(10*(i-1))
    

提交回复
热议问题