Python-based image capture program 1.0.00 and python1.0.00
# Coding = gbk
Import urllib
Import urllib2
Import re
Import OS
Import time
# Import readline
Def getHtml (url ):
# Some websites restrict browser access, and python simulates browsers
Heads = {'access': 'text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 100 ',
'Accept-charset': 'gb2312, UTF-8; q = 0.7, *; q = 100 ',
'Accept-color': 'zh-cn, zh; q = 66661 ',
'Cache-control': 'max-age = 0 ',
'Connection': 'Keep-alive ',
'Host': 'john ',
'Keep-alive': '123 ',
'Referer': url,
'User-agent': 'mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv: 1.9.2.14) Gecko/20110221 Ubuntu/10.10 (maverick) Firefox/3.6.14 '}
Opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor ())
Urllib2.install _ opener (opener)
Req = urllib2.Request (url)
Opener. addheaders = heads. items ()
RespHtml = opener. open (req). read ()
# Return respHtml. decode ('gbk'). encode ('utf-8 ')
Return respHtml
Def getImg (html ):
# Reg = r'input src = \'*(.*? \. Jpg )'
Reg = r'src = "(. +? \. Jpg )"'
Imgre = re. compile (reg)
Imglist = re. findall (imgre, html)
# Print (imglist)
# Return 1
X = 0
# Generate temporary directory Storage
CreateDir = 'getpic '+ time. strftime (' % Y % m % d % H % M % s ')
If not OS. path. isdir (createDir) and not OS. path. isfile (createDir ):
OS. mkdir (createDir)
# Print (OS. getcwd ())
OS. chdir (createDir)
# Print (OS. getcwd ())
For imgurl in imglist:
Print ("capturing images:" + imgurl)
Urllib.urlretrieve(imgurl,'{s.jpg '% x)
X + = 1
# Readline. parse_and_bind ("control-v: paste ")
Website = raw_input ("please input website :")
Html = getHtml (website)
# Print (html)
GetImg (html)
I recently learned python and made a screenshot program by referring to the online documents. The following points need to be improved:
1. How to support interface input, such as multiple options and multiple URLs
2. The matching list is supported, and the matching rules of some websites are found to be inconsistent, resulting in unavailability.
3. winpython does not support the readline module. As a result, the cmd window generated by py2exe must be set before the URL can be pasted.
If you use py2exe to generate a common exe file
Create a file create. py in the working directory. The content is as follows:
From py2exe. build_exe import py2exe
From distutils. core import setup
From glob import glob
Import py2exe
Import OS, sys
Import shutil
If len (sys. argv) = 1:
Sys. argv. append ("py2exe ")
Includes = ["encodings", "encodings. *"]
Options = {"py2exe ":
{"Compressed": 1,
"Optimize": 2,
"Primary des": Primary des,
"Dist_dir": "bin ",
"Bundle_files": 1
}
}
Setup (
Version = "1.0 ",
Description = u'to grab Pictures ',
Name = "grabpic1.0.00 ",
Options = options,
Zipfile = None,
Console = [{"script": "grabpic1.0.00.py"}],
Data_files = []
)
OS. remove ("bin // w9xpopen.exe ")
# Shutil. rmtree ("build ")
Run the python create. py py2exe command to generate the file.
Run grabpic1.0.00.exe, right-click it, and you will see an insert mode in the middle. Select the check box, and then the cmd window will be able to map.
PS: The first write, do not know how to map, sad reminder
Reference post:
Http://blog.csdn.net/txg703003659/article/details/30459475
Http://blog.csdn.net/linda1000/article/details/12909439