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

  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
The discussion focuses on how to iterate through a linked list in Python using the urllib library and suggests using the Requests library for better functionality. Participants highlight the need to extract a number from the response and use it to form a new URL for subsequent requests. They recommend using f-strings or string concatenation to handle variable insertion in URLs. The Requests library is favored for its ease in passing query parameters and handling responses. Overall, the conversation emphasizes the importance of using modern libraries for web requests in Python.
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 ?
 
Technology 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 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 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
3
Views
4K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K