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
Click For Summary

Discussion Overview

The discussion revolves around how to continuously iterate through a linked list in Python using the urllib library, particularly in the context of a specific challenge from a website. Participants explore methods to extract and utilize dynamic URL parameters based on responses from the server.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares initial code using urllib to make a request and print the response, which includes a number to be used in a subsequent request.
  • Another participant suggests using the Requests library instead of urllib, citing it as more modern and easier for handling variables in URLs.
  • A third participant echoes the recommendation for the Requests library, providing an example that demonstrates how to pass query parameters more conveniently.
  • One participant comments on the website's design, noting its age and the potential obsolescence of the model answers provided, suggesting caution in relying on them.

Areas of Agreement / Disagreement

There is no consensus on the best approach, as participants express differing opinions on the use of urllib versus the Requests library, and some participants critique the website's instructional quality.

Contextual Notes

Participants mention the limitations of using outdated libraries and the potential for deprecated methods in the context of the challenge.

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 7 ·
Replies
7
Views
5K
  • · 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
2K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K