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

  • Thread starter Arman777
  • Start date
  • Tags
    Python
In summary, the conversation discusses the use of the urllib library to make requests and handle variables in strings. It also mentions the use of the Requests library as a better alternative. The conversation also notes the PythonChallenge website's use of PHP and the potential issues with using the deprecated model answers and wiki.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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
  • #3
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
  • #4
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.
 

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

1. What is urllib in Python?

Urllib is a Python library that allows you to make HTTP requests and interact with web pages or URLs. It provides a simple and easy-to-use interface for retrieving data from URLs.

2. How do I import urllib in Python?

To use urllib in Python, you first need to import it into your code. You can do this by using the following code:
import urllib
If you want to use specific modules from the urllib library, you can import them individually, for example:
from urllib import request

3. How do I make a GET request using urllib?

To make a GET request using urllib, you can use the urlopen() function from the urllib.request module. This function takes in a URL as its argument and returns an HTTPResponse object, which contains the response from the server. Here's an example of making a GET request and printing the response:

response = urllib.request.urlopen('https://www.example.com')
print(response.read())

4. How do I make a POST request using urllib?

To make a POST request using urllib, you need to use the urllib.request.urlopen() function and pass in a data parameter with the data you want to send in the request. Here's an example:

data = {'name': 'John', 'age': 25}
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen('https://www.example.com', data)
print(response.read())

5. How do I handle errors in urllib requests?

To handle errors in urllib requests, you can use the urllib.error module. This module contains various exceptions that can be raised when there is an error in the request, such as HTTPError or URLError. You can use a try/except block to catch these errors and handle them accordingly.

Similar threads

  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
7
Views
537
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
3
Views
3K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
4
Views
942
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top