![]() |
urllib and save pictures
Hi.
This is my first post, and is about an assignment I've at my college. An overall description: We have to make a function, with one argument, the URL. then we have to search the HTML code for any pictures, and to do that I will search for <img and src tags. All that I can, but then we have to save the pictures local on my harddrive, and make a collage with all the pictures in it. My hindrance right now is the saving part. For testing the script, I'm using this code: :
def getImageUrl(urlstring):I know my code isn't optimized, but I just can't seem to find the function, so it will save my pictures... In advanced thanks. Greetings Public2 |
You're on the right track, but there are three problems that I can see with your code. Firstly, you appear to be looking for a 'src' tag, when it's an attribute. Secondly, you're trying to open a file named picture, where picture is a variable containing your HTML page. Thirdly, you're not getting the URL of the image, you're getting the URL of the page again.
Whenever I'm doing any work with HTML in Python, I use Beautiful Soup. It's wonderfully easy to use, and comes as a single py file, so it's really rather good. Using Beautiful Soup, your function might look like: :
|
Hey Arevos.
Thanks for your answer, I just got one problem that is, I don't think we are allowed to import external codes like BeautifulSoup. My code can detect that there is pictures in the HTML code, but I just can't seem to save them to my harddrive. I'll try to make the code work, but it is more difficult then I thought it would be. |
If you've already got the "src" attribute, you can just use the inner-most indentation of the previous code:
:
:
|
Hey again.
I finally got finished with my assignment, and thought I would write the code down here. It turned out that we had to make most of the code in Jython, so some of the modules couldn't be used, but I managed anyway. Here is the complete code: :
import urllibHave a great evening. Greetings Public2 |
| All times are GMT -5. The time now is 12:43 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC