Python crawlers crawl the image address instance code on a webpage,

Source: Internet
Author: User

Python crawlers crawl the image address instance code on a webpage,

The example in this article is to crawl an image address on a webpage, as shown below.

Read the source code of a web page:

Import urllib. requestdef getHtml (url): html = urllib. request. urlopen (url). read () return htmlprint (getHtml (http://image.baidu.com/search/flip? Tn = baiduimage & ie = UTF-8 & word = % E5 % A3 % 81% E7 % BA % B8 & ct = 201326592 & lm =-1 & v = flip ))

Use a regular expression to crawl an image address on a webpage:

Import reimport urllib. requestdef getHtml (url): html = urllib. request. urlopen (url ). read () return htmldef getImg (html): r = R' "thumbURL": "(http: // img. +? \. Jpg) "'# define regular imglist = re. findall (r, html) return imglisthtml = str (getHtml (" http://image.baidu.com/search/flip? Tn = baiduimage & ie = UTF-8 & word = % E5 % A3 % 81% E7 % BA % B8 & ct = 201326592 & lm =-1 & v = flip ")) print (getImg (html ))

Running result:

Summary

The above is all the content about the Python crawler crawling the image address instance code on a web page. I hope it will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.