Retreiving data from a website [duplicate]

2024/7/7 5:47:01

I'm terribly sorry if this is unacceptable or answered elsewhere, but I've spent the last hour and a half looking for information on it, and have come up with nothing I can use. I'm brand spanking new to Python and have been given an assignment to pull an IP from a site. I'm able to get my program to read the site, but I'm simply unable to figure out what to do next. Every answer that remotely comes close to what I want is beyond my programming capabilities to the point that I just don't understand it, and tutorials don't actually get at the specific problem I'm having. Again, if there's any document or text I can read instead of wasting time, please send me that way.

import urllib.request
site = urllib.request.urlopen("http://homer.wcitac.org/~sec290/hwk2/")
print (site.read())
IP = site[10]
print (IP)

I know, it's simplistic, but I've only been doing this for a little while. As far as I can tell, it should print back the 10th (9th on the page) character so I have a starting point, so I can then use a colon to find the characters I want, but it's giving me "TypeError: 'HTTPResponse' object does not support indexing", and I've no idea what that means.

Answer

You have the site variable pointed to the return value of urllib.request.urlopen. In the next line, you call site.read(), which returns a string. In short, site is not referencing a string; it's referencing a response object which can be used to get the string content.

Since you already know site.read() returns a string, why not capture that as a variable and use it?

content = site.read()
print(content)
https://en.xdnf.cn/q/120364.html

Related Q&A

How to comma separate an array of integers in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 2.7.5 - Where is it installed on Windows Vista? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…

Python list of lists specific path combinations or permutations

I have a list of lists and am looking for something similar to combinations or permutations, but there are conditions that may result in good "Path" or "Dead_End". If "Dead_En…

Python packages.import sys vs from sys import argv

Im trying to use argv into my python script. For this im using following code:from sys import argv script, file_name = argv print(file_name)python3 e.py e.txt This code works fine.But when I use:import…

How to reorganize a list of tuples?

Say I had a list of tuples:[(98, studentA), (97, studentB), (98, studentC), (95,studentD)]And I wanted to organize it so that the students are grouped together by the first number in the tuple, what wo…

How to loop through json data with multiple objects

My json file data.json looks like this [ {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"}, {"host"…

python django only the first statement statement can be accessed

i can acccess only the first statement in my name appjavascript:<script type="text/javascript">function searched(){{% for names in name %}nameSearched = document.getElementById(name).va…

syntax error return outside function in python

I am trying to count the word fizz using python. However it is giving me an error.def fizz_count(x):count =0 for item in x :if item== "fizz":count=count+1 return countitem= ["fizz",…

How to replace values in multidimensional array?

I am trying to get a multidimensional array working, where the user string is filled in the cell. I have been searching for ways to update user values in the multidimensional array def createMultiArr…

How to deploy a python docker image to AWS Lambda?

I am trying to figure out how to deploy a flask application that I have received with a Dockerfile to AWS Lambda.In local, all I have to do to start the app is to enter docker-compose up. Thats work gr…