Python Learning---web crawler [download image]

Source: Internet
Author: User

Crawler Learning--Download images

1. The urllib and re libraries are used mainly

2. Use the Urllib.urlopen () function to get the page source code

3. Use regular matching image type, of course, the more accurate, the more downloaded

4. Download the image using Urllib.urlretrieve () and rename it using%s

5. There should be restrictions on the operator, so it is not possible to download all the pictures, but OK

URL Analysis:

Source:

#coding =utf-8import reimport urllibdef gethtml (URL):    page=urllib.urlopen (URL)    html=page.read ();    return htmldef getImage (HTML):    reg=r ' src= ' (. *?\.jpg) "Size '    imgre=re.compile (reg)    imgelist =re.findall (imgre,html)    X=0    for image in Imgelist:        urllib.urlretrieve (image, '%s_hhh.jpg '% x)        x+=1html=gethtml ("https:// tieba.baidu.com/p/5256641773 ") getImage (HTML)

Python Learning---web crawler [download image]

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.