Python---Search the contents of HTML files, take a specific URL address string, save it as a list, download the picture with each URL, and save it to your hard disk, using the regular re

Source: Internet
Author: User
Tags python script kali linux aliyun

python-the contents of the HTML file to retrieve a specific URL address string, save it as a list, and use each URL to download the picture and save it to the hard drive, the regular re

Reference: http://blog.csdn.net/xwbk12/article/details/72734930

1, the target address: https://xianzhi.aliyun.com/forum/topic/1805/
The contents of the following figure

Remove the contents of the target back package like this:
https://xianzhi.aliyun.com/forum/media/upload/picture/20171215230019-ab0e46aa-e1a8-1.png

2. Python Script
Running on Kali Linux

root@kali:~/python# cat downloadxianzhi-re.py 

#coding =utf-8  
import urllib  
import re  
import sys  

def gethtml (URL):  
    page = urllib.urlopen (URL)  
    html = page.read () return  
    HTML  

def getimg (HTML):  
    reg = R ' src= ' (. +?\.png) ' ></p> '  
    imgre = Re.compile (reg)  
    imglist = Re.findall (imgre,html)  
    x = 0  
    for Imgurl in imglist:  
        urllib.urlretrieve (Imgurl, '%s100.jpg '% x)  
        x+=1 return  
    imglist  

html = gethtml ("https://xianzhi.aliyun.com/forum/topic/1805/")  

print getimg (HTML)

3. Operation situation

Src= "(. +?\.png)" ></p>
Explanation:
src= "           #匹配src ="
(. +?\.jpg)
# brackets denote grouping, capturing the contents of parentheses into groups
#. + to match at least one arbitrary character, question mark? Indicates lazy matching, which is to match as few strings as possible.
#  . +?\.jpg together to represent as few matching characters as possible to. jpg, to avoid matching the range beyond the SRC range
#  This bracket can also match the URL of the picture in the page
"" ></p >         #匹配 "></p>
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.