Python Simple crawler

Source: Internet
Author: User

using the Urllib.request and re modules
1 fromUrllib.requestImport*2 ImportRe#Handling Network Access3 #get Web page4URL ='Https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word =%c3%c0%c5%ae%cd%bc%c6%ac&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111'5 #Open Web page6HMTL =urlopen (URL)7 #get HTML code, decode decode8obj =Hmtl.read (). Decode ()9 #use RE to find all the Objurl links. * Match all resultsTenURLs = Re.findall (r'" Objurl": "(. *?)"', obj) Oneindex = 1 A forwr.inchURLs: - Try: - ifRe.search ('. jpg$', URL): the Print('downloading........%d'%index) -Urlretrieve (URL,'pic'+STR (Index) +'. jpg') - Else: - Print('downloading........%d'%index) +Urlretrieve (URL,'pic'+ STR (index) +'. PNG') -Index + = 1 + A exceptException: at Print('Download error....%d'%index) - Else: - Print('Download complete')

Crawl a picture

Using the Requests module
1 Import requests2'http://www.cnblogs.com/Images/Skins/BJ2008.jpg' 3 response = Requests.get (image_url)4 with open ('outlook.jpg ','wb') as F:5 f.write (response.content)

Python Simple crawler

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.