#coding=gbk
from xml.dom import minidom,Node
import urllib2,re,os
def readsrc(src):
try:
url = urllib2.urlopen(src)
content = url.read()#.decode('utf-8')
return content
except:
print 'error'
return None
def pictype(content):
'''
通过抓取网站导航栏,获得网站的图片类型
返回列表,每个列表元素为一个字典,addr代表图片类型对于的链接,name代表图片类型的名称
错误会返回None
'''
p = re.compile(r'
- (.*)
r=p.search(content)
if r:
content=r.group()
else:
print None
p = re.compile(r'
l = [i.groupdict() for i in p.finditer(content)]
l=l[1:]
if len(l):return l
else:return None
def pageinfo(src):
'''
获取一个页面的详细信息
返回对于的字典列表
name:图片的名字
cutaddr:缩小的浏览图
picaddr:实际图片的地址
'''
d=os.path.split(src)[0]
try:
url = urllib2.urlopen(src)
content = url.read()#.decode('utf-8')
except:
print 'error'
return None
#find all the pictures info in a page
p = re.compile(r'
r = p.findall(content)
if not r: return None
r = r[1]
p = re.compile(r'

l = [ i.groupdict() for i in p.finditer(r)]
for i in l:
i['picaddr']=d+'/'+i['picaddr']
if len(l): return l
else: return None
def nextpageaddr(src):
'''
从页面的html源码中获取下一个页面地址的名称,最后一页返回None
'''
content=readsrc(src)
p = re.compile(r'.*?')
r = p.search(content)
if r:
return os.path.dirname(src)+"/"+r.group(1)
else:
return None
def picinfoaddr(src):
'''
参数相册图集的html代码
返回全部图片的相对地址
'''
content=readsrc(src)
p = re.compile(r'
r = p.search(content)
if r:
return os.path.dirname(src)+"/"+r.group(1)
else:
return None
def parseinfo(content):
'''
读取全部图片html代码,获得一个相册的详细信息
kw:关键字
title:标题
type:类型
pic:各个图片的地址列表,末尾加上_220x165,_medium,_small 可以得到不同大小的图片
'''
info={}
temp=str()
#title
temp=''
r=re.search('
(.*?)
',content)#get the pic titleif r:
temp = r.group(1)
info['title']=temp
#keyword
temp=''
r=re.search('',content)
if r:
temp = r.group(1)
info['kw']=temp
#type
r=re.findall('
if r:
info['type']=':'.join(r)
else:
info['type']=''
r=re.search('
- (.*?)
if not r:return None
content=r.group(1)#filter content
# print content
r=re.findall('',content)
for index,i in enumerate(r):
r[index]=i[0:i.rfind('_')]
# print r[index]
info['pic']=r
return info
import threading
class mthread(threading.Thread):
def __init__(self,tp,addr,lock):
threading.Thread.__init__(self)
# self.doc = minidom.Document()
self.doc=minidom.Document()
self.tp=tp
self.lock=lock
self.addr=addr
self.thread_stop=False
self.picdoc=None
def run(self):
self.picdoc = self.doc.createElement('urlclass')
# print self.tp
self.picdoc.setAttribute('type',self.tp)
# self.doc.appendChild(self.picdoc)
m=pageinfo(self.addr)
while self.addr:
for i in m:
# print i['picaddr']
picaddr=picinfoaddr(i['picaddr'])
# print picaddr
info=parseinfo(readsrc(picaddr))
name=info['title']
picture=doc.createElement('picture')
title = doc.createElement('title')
title.appendChild(doc.createTextNode(info['title']))
picture.appendChild(title)
keyword = doc.createElement('keywords')
keyword.appendChild(doc.createTextNode(info['kw']))
picture.appendChild(keyword)
tp = doc.createElement('pictype')
tp.appendChild(doc.createTextNode(info['type']))
picture.appendChild(tp)
cuturl = doc.createElement('piccut')
cuturl.appendChild(doc.createTextNode(i['cutaddr']))
picture.appendChild(cuturl)
urls = doc.createElement('urls')
self.lock.acquire()
print 'downloading ',name
self.lock.release()
for picurl in info['pic']:
singleurl=doc.createElement('url')
singleurl.appendChild(doc.createTextNode(picurl+'.jpg'))
urls.appendChild(singleurl)
picture.appendChild(urls)
self.picdoc.appendChild(picture)
m=pageinfo(self.addr)
self.addr=nextpageaddr(self.addr)
# f = open('c:\\'+self.tp+'.xml','w')
# f.write(doc.toprettyxml(indent = ''))
# f.close()
def stop(self):
self.thread_stop=True
path='C:\\pict\\'#下载的路径
#import sys
sys.exit(12)
content=readsrc('http://photos.pcgames.com.cn/cate/3/1.html')
r=pictype(content)
lt=[]
doc = minidom.Document()
root=doc.createElement('url_resource')
root.setAttribute('type','url')
root.setAttribute('urltype','image')
root.setAttribute('imgfmt','jpg')
doc.appendChild(root)
lock=threading.RLock()
for iaddr in r:
print 'downloading type: ',iaddr['name']
addr=iaddr['addr']
th=mthread(iaddr['name'],addr,lock)
lt.append(th)
th.start()
for t in lt:
t.join()
root.appendChild(t.picdoc)
print 'write'
f = open('c:\\'+'urls'+'.xml','w')
f.write(doc.toprettyxml(indent = ''))
f.close()
print doc.toprettyxml()
print 'end'

There are many methods to connect two lists in Python: 1. Use operators, which are simple but inefficient in large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use the = operator, which is both efficient and readable; 4. Use itertools.chain function, which is memory efficient but requires additional import; 5. Use list parsing, which is elegant but may be too complex. The selection method should be based on the code context and requirements.

There are many ways to merge Python lists: 1. Use operators, which are simple but not memory efficient for large lists; 2. Use extend method, which is efficient but will modify the original list; 3. Use itertools.chain, which is suitable for large data sets; 4. Use * operator, merge small to medium-sized lists in one line of code; 5. Use numpy.concatenate, which is suitable for large data sets and scenarios with high performance requirements; 6. Use append method, which is suitable for small lists but is inefficient. When selecting a method, you need to consider the list size and application scenarios.

Compiledlanguagesofferspeedandsecurity,whileinterpretedlanguagesprovideeaseofuseandportability.1)CompiledlanguageslikeC arefasterandsecurebuthavelongerdevelopmentcyclesandplatformdependency.2)InterpretedlanguageslikePythonareeasiertouseandmoreportab

In Python, a for loop is used to traverse iterable objects, and a while loop is used to perform operations repeatedly when the condition is satisfied. 1) For loop example: traverse the list and print the elements. 2) While loop example: guess the number game until you guess it right. Mastering cycle principles and optimization techniques can improve code efficiency and reliability.

To concatenate a list into a string, using the join() method in Python is the best choice. 1) Use the join() method to concatenate the list elements into a string, such as ''.join(my_list). 2) For a list containing numbers, convert map(str, numbers) into a string before concatenating. 3) You can use generator expressions for complex formatting, such as ','.join(f'({fruit})'forfruitinfruits). 4) When processing mixed data types, use map(str, mixed_list) to ensure that all elements can be converted into strings. 5) For large lists, use ''.join(large_li

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
