How to Continuously Iterate Through a Linked List in Python Using urllib

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Python
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
Code:
import urllib.request

import urllib.request
request_url = urllib.request.urlopen(
    'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345')
print(request_url.read())

I have a code somthing like this which prints

"and the next nothing is 44827"

Now is there a way to get that number and put it into the url ? becasue in that case I ll obtain another number and I need to also put that in that url ?
 
Physics news on Phys.org
I would use the Requests library instead. urllib is a little outdated.

There are many ways to handle variables in strings (look into f strings), but in a pinch you can always use concatenation.

Python:
idnum = 12345

request_url = urllib.request.urlopen(
    'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + idnum)
 
  • Like
Likes   Reactions: optimisticsoda and Arman777
What a strange website: it purports to teach Python but it seems that any language that can make requests can be used, and the site itself is written in PHP!

Given that solving the puzzles requires making repeated requests I strongly echo @Greg Bernhardt's recommendation of Requests - it makes things like passing query parameters much easier.

Python:
import requests
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php'
vars = { 'idnum': 12345 }
req = requests.get(url, params = vars)

# http://www.pythonchallenge.com/pc/def/linkedlist.php?idnum=12345.
print(req.url)
# The text body of the response.
print(req.text)
 
Last edited:
  • Like
Likes   Reactions: Arman777 and Greg Bernhardt
pbuk said:
What a strange website: it purports to teach Python but it seems that any language that can make requests can be used, and the site itself is written in PHP!
... and it's 15 years old which explains why the model answers use urllib and no doubt other things that are now deprecated or broken. I'd stay away from the model answers and the wiki.