473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to store soucre code from urlopen?

hi i can print out the website source code but i cant store whats been read in a variable as a string so is there away of doing this?

Expand|Select|Wrap|Line Numbers
  1.  
  2. with urllib.request.urlopen(path) as url:
  3.     sourcecode = url.read()
  4.     print(sourcecode)#prints source code
  5.     sourcecode = storecode #i cant do this???
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
Jan 7 '12 #1
6 2200
bvdet
2,851 Expert Mod 2GB
From your code, it appears you are using Python 3.x. Since I don't have Python 3 installed, I have no way of testing your code. Could your problem have something to do with the the context in which your code is executed? Is it possible the file object __exit__() method is garbage collecting local variable sourcecode? Just guessing.

Try executing s = urllib.request.urlopen(path).read() without using the with statement
Jan 8 '12 #2
Hi thanks for your help i tried it without the with statement it is still not working i only seem able to print out from whats being read that's all

my code is fairly simple it only asks the user for the url then reads in the source code from the url entered after that i want to store whats being read into a string variable for further manipulation but i haven not figured a way to do that.
Jan 8 '12 #3
bvdet
2,851 Expert Mod 2GB
Acccording to Python docs, urllib.request.urlopen(url[, data][, timeout]) returns a file like object with 2 additional methods defined. As you know, a file object's read method returns the file contents as a string. I keep thinking you may be defining a local variable which is unavailable in the global scope. Try creating a global variable with the global statement.
Jan 8 '12 #4
sorry did you mean, setting s as a global variable?
Expand|Select|Wrap|Line Numbers
  1. def s():
  2.   global s
  3.   s = urllib.request.urlopen(path).read()
  4.  
Jan 8 '12 #5
dwblas
626 Expert 512MB
You have the variables reversed.
Expand|Select|Wrap|Line Numbers
  1. with urllib.request.urlopen(path) as url:
  2.      sourcecode = url.read()
  3.      print(sourcecode)#prints source code
  4.      ##sourcecode = storecode #i cant do this?
  5.      storecode = sourcecode 
Jan 8 '12 #6
i have found the problem, thanks for your help, the problem is that whats been read from url.read is not stored as a string once i converted the variable to a string i could manipulate the string thanks again i appreciate your help.
Jan 8 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Wayne Wengert | last post by:
In VB6 I used to put Public variables, functions and Subs in a BAS Module. What is the best way to handle this in .NET? From what I see in some books it looks like I should use a Class Library but...
3
by: Wayne... | last post by:
I'm trying to make a sort of source code libary for some customers of commonly used code to save them a bit of time the problem I have is that although I can get the asp code into a field of an...
1
by: Mr. Cheung | last post by:
Hi All, I use .NET 1.1 vbc.exe command : vbc /r:xxx.dll /t:library test.vb to general a test.dll. This library provides some web custom controls for ASP.NET developer use. I don't use the...
6
by: Peter Afonin | last post by:
Hello, I'm creating something like a web site builder in ASP.Net, and I need to store an HTML code in the SQL server table. If my customer just enters an HTML code, it is stored in the SQL...
7
by: Alan Silver | last post by:
Hello, I am just looking at VWD and seeing what needs doing to take an existing site I've written by hand and importing it into VWD. I've already discovered that I need to rename my code-behind...
4
by: William | last post by:
After much frustration I was able to update my data store via code only. Using the data adapter was the only way I was able to set up all the objects written in my code. Basically, I cheated by...
6
by: JR | last post by:
I'm writting a program and in 1 form-module I get this error when there is really something wrong. this way it is not easy to find the pleace where I made a mistake. How can I solve this? ...
1
by: Artificer | last post by:
Allow me to explain my question. I would like create a function in which one of it sentences will be decided at run time. Something like String MySentence= "int x=0;" Public void...
1
by: sivakrishna546 | last post by:
using System; using System.Collections; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace...
2
by: TimSki | last post by:
Hi, I can easily display the page source by simply calling window.location = "view-source:http://www.example.com"; but how can i store the source code into a variable which i can work with....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.