| | |
urllib2 problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi, I have this code:
and whenever I run it, it raises this error:
What is the problem, but mainly what can I do to fix it?
thanks in advance
python Syntax (Toggle Plain Text)
import urllib2 as url import webbrowser def extract(text, sub1, sub2): """ extract a substring from text between first occurances of substrings sub1 and sub2 """ return text.split(sub1, 1)[-1].split(sub2, 1)[0] start="http://xkcd.com/" permlist=[] textlist=[] for i in range(1, 638): temp=start+str(i) permlist.append(str(url.urlopen(temp).readlines()[88])) textlist.append(str(url.urlopen(temp).readlines()[77])) for i in permlist: i = extract(i, '<h3>Permanent link to this comic: ', '</h3>') for i in textlist: i = extract(i, '<img src="http://imgs.xkcd.com/comics/scribblenauts.png" title="', '"') print zip(permlist, textlist)
and whenever I run it, it raises this error:
Python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "C:/Python26/test.py", line 15, in <module> permlist.append(str(url.urlopen(temp).readlines()[88])) File "C:\Python26\lib\urllib2.py", line 124, in urlopen return _opener.open(url, data, timeout) File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response) File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python26\lib\urllib2.py", line 427, in error return self._call_chain(*args) File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args) File "C:\Python26\lib\urllib2.py", line 510, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 404: Not Found
What is the problem, but mainly what can I do to fix it?
thanks in advance
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
•
•
Join Date: Aug 2009
Posts: 18
Reputation:
Solved Threads: 5
python Syntax (Toggle Plain Text)
for i in range(1, 638): try: temp=start+str(i) permlist.append(str(url.urlopen(temp).readlines()[88])) textlist.append(str(url.urlopen(temp).readlines()[77])) except Error: # catch any exception and continue the for loop print "Error at index %d."%i
•
•
Join Date: Sep 2009
Posts: 108
Reputation:
Solved Threads: 12
Yeah you'll need to use exceptions, but if you want the script to continue after the error you're going to have to "pass" it, try this:
this will not only print the error and the location of the error but will also pass to keep the loop going.
Python Syntax (Toggle Plain Text)
for i in range(1, 638): try: temp=start+str(i) permlist.append(str(url.urlopen(temp).readlines()[88])) textlist.append(str(url.urlopen(temp).readlines()[77])) except Error, err: print "Index Error: %d at %d" % (err, i) pass
this will not only print the error and the location of the error but will also pass to keep the loop going.
hey again, they are good ideas but whenever I try to run it again it says:
Python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "C:\Python26\test.py", line 18, in <module> except Error, err: NameError: name 'Error' is not defined
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
Since you don't know the specific error class, simply use ...
python Syntax (Toggle Plain Text)
for i in range(1, 638): try: temp=start+str(i) permlist.append(str(url.urlopen(temp).readlines()[88])) textlist.append(str(url.urlopen(temp).readlines()[77])) except: # catch any exception and continue the for loop print "Error at index %d."%i pass
May 'the Google' be with you!
Oh ok thanks, whenever I run the zip part it uses the original text, not what I changed it to, I tried:
but it says that it is out of range, what can I do? I have googled it to no avail
Python Syntax (Toggle Plain Text)
for i, j in permlist, textlist: print i, ':', j
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
![]() |
Similar Threads
- Searching Weather for a term with urllib2 module (Python)
- I had problem with onmouse out in div (JavaScript / DHTML / AJAX)
- urllib2 fetching different page than browser (Python)
- HTML table parse problem (beautiful soup) (Python)
- MySQLdb interface problem (MySQL)
- How to set timeout for reading from urls in urllib (Python)
- HTML Scraper: Urllib2 / BeautifulSoup / Regex Help (Python)
- urllib2 help (Python)
Other Threads in the Python Forum
- Previous Thread: Open directory dialog box
- Next Thread: Caeser cipher code breaker help (homework)
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv array beginner book change code color converter countpasswordentry dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






