Python Crawl Tour Discover albums

Source: Internet
Author: User

With the urllib.request provided by Python3 it is easy to crawl things on the web.

1. Urllib.request.urlopen (URL) opens the webpage and reads read ()

2, Python regular analysis of image links, such as <photo= ' http://img3A.hualvtu.com/272492/20150223/2143e9d2b51b397cda16.jpg ' >

3, Urllib.request.urlretrieve (URL, filename) download the corresponding URL image, save to filename

Plus create file directory Os.makedirs (), Log.txt text record

See the code in detail:


# coding = utf-8# by Qiuimport Re, osimport urllib.requestpage = ' http://fm.hualvtu.com/viewQuark.action?id= 10150223231300000165&un=woshiyyh&reply=false ' # download htmldef download_html (URL): HTML = Urllib.request.urlopen (URL). Read () return Html.decode () def getImage (HT): REG = R ' photo=\ ' (. *?\.jpg) \ ' dt= ' obj =        Re.compile (reg) imglist = Re.findall (obj, ht) folder = ' g:/download/photos/' If not os.path.exists (folder): Os.makedirs (folder) logfile = open (folder+ ' log.txt ', ' wt ') logfile.write (' Image download source ' + page + ' \ n ') s = 1 for I In Imglist:try:print (' Download%d pictures ...            '% s ') Urllib.request.urlretrieve (i, folder+ ' pic%s.jpg '% s) except:print ("Download Error") Logfile.write (i+ ' download error \ n ') continue Logfile.write (' picture%d link--'% s+ i + ' \ n ') s + = 1 logfile.close () print (' End of download ') HTML = download_html (page) getImage (HTML)
Painting Journey

Python crawl tour Discover albums

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.