How to download the image of the network and generate HTML in bulk _javascript tips

Source: Internet
Author: User
Tags python script

Use Ajax to implement asynchronous load content, in its JS code to find the relevant code

Type: ' POST ',
    URL: '/index.php/request/new_data2/' + times + '/' +locinfo[domn][0],
    dataType: ' JSON ',

The returned JSON string is a serialized array in which the dictionary is stored, which concerns dict[' t ' and dict[' i '],dict[' t ' to hold the description of the picture, dict[' I '] The URL of the picture is stored. Once you know this, you can start the Python script.

Import related Modules

#-*-Coding:utf-8-*-
import urllib2 as URL
import JSON
import sys
import OS from
datetime import *

(fixed cannot get a specified type of bug, the last digit in the requested URL represents the type)

Get Json:index is the first page of download, type is tws (too wretched) tr (too Hot) tgx (too Funny) tml (too cute) tht (too beautiful tyy (too) one

Then you create the HTML file

def create_html (alllist,name):
  html_head= '  
 

Download pictures

def download (list,dirname,index=0):
  os.chdir (dirname) for
  dict in list:
    imgurl=dict[' i ']
    text= dict [' t ']
    Print index
    print imgurl
    print text
    res=url.urlopen (imgurl)
    img_type= ". gif"
    content_type= res.headers["Content-type"]
    if content_type== "image/jpeg":
      type= ". Jgp"
    filepath= "%s"% (str (index) +img_type)
    F=open (filepath, "WB")
    F.write (Res.read ())
    f.close ()
    res.close ()
    index+=1
  Os.chdir (".. /")

The main function, used to invoke the above functions

def start (type,lenght):
  lenght=int (lenght)
  now=datetime.now ()
  now=now.strftime ("%m-%d%h.%m.%s")
  Os.mkdir (Type+now)
  
  alllist=[] for
  x in range (0,lenght):
    List=get_json (X,type)
    Alllist.extend (list)
  create_html (alllist,type+now)
  Download (alllist,type+now)
  print "\r\n\r\n=== ===========ok==============\r\n\r\n "

A loop body that gets the user input

while (True):
  print Enter TWS (too wretched) tr (too Hot) tgx (too Funny) tml (too cute) tht (too beautiful tyy (too) one \r\nexit: Exit "
  type=raw_input" ()
  all_type=["TGX", "TWs", "TYy", "tr", "tml", "tht"]
  if type in All_type:
    print "type download pages:"
    lenght=raw_input ()
    Start (type,lenght)
  elif type== "exit":
    break
  else:
    print "\ r \ n input error \ r \ n"

OK, the script will generate a time-named HTML file and a folder with the same name to store the picture in the current directory.

Test, download more than 100 pictures in a few minutes, so I do not feel the need for more threads to download.

can also be slightly modified to generate HTML where the page is displayed, and then drag the Web page in Zhuo phone look is also good

It's so easy with Python!

The above batch download to the road network picture and generate the HTML realization method is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.