python中的线程、正则表达式
2016-04-18 14:33
267 查看
一、python中的线程复杂度较高,不提倡用,看了几个视频,觉得跟Java的定义和使用方式大同小异,不得不说,python和Java的相似度还是蛮高的。用python多线程的地方:抓取网页时候url分析、数据库连接池。
二、python中的正则表达式与别的语言一样,在抓取网页的时候分析url地址中常常被使用。只有将其用在实战的场景中才会去想怎么用好,为了不至于博文太空虚,还是贴一下,前段时间写的给女朋友抓取网上图片的小爬虫吧,我知道写的很渣,实际上那时候对于Python还是不太懂:
import urllib2
import re
import os
import uuid
import urllib
def getHtml(url):
page=urllib2.urlopen(url)
html=page.read()
return html
def deleteDuplicatedElementFromList2(list):
resultList = []
for item in list:
if not item in resultList:
resultList.append(item)
return resultList
def getLink(html):
reg=r'href="http://www.amazon.com/Diesel\S*refinements=p_89%3ADiesel'
'''
'''
htmlLink=re.compile(reg)
Link=re.findall(htmlLink, html)
lastLink=deleteDuplicatedElementFromList2(Link)
return lastLink
def getlastlink(html):
reg=r'http://demandware.edgesuite.net/aatd_prd/on/demandware.static/-/Sites-cog-men-master/default/\S*jpg'
'''
src="http://ecx.images-amazon.com/images/I/91sFieFTvbL._UY879_.jpg"
'''
htmlLink=re.compile(reg)
Link=re.findall(htmlLink, html)
lastLink=deleteDuplicatedElementFromList2(Link)
return lastLink
def createFileWithFileName(localPathParam,fileName):
totalPath=localPathParam+'\\'+fileName
if not os.path.exists(totalPath):
file=open(totalPath,'a+')
file.close()
print totalPath;
return totalPath
def getAndSaveImg(imgUrl,name):
if( len(imgUrl)!= 0 ):
urllib.urlretrieve(imgUrl,createFileWithFileName(Path,name))
print('saved successed!')
Path='d:\\pythonp'
html1="http://www.amazon.cn/s/ref=lp_2154530051_pg_"#"http://www.amazon.com/gp/search/ref=sr_pg_"
html2="?rh=n%3A2016156051%2Cn%3A%212016157051%2Cn%3A2152155051%2Cn%3A2154530051&page=2&ie=UTF8&qid=1451709046";
i=0
j=1
while j<2:
htmls=html1+str(j)+html2;
print htmls
#print getHtml(htmls)
deeperlink=getLink(getHtml(htmls))
print len(deeperlink)
for link in deeperlink:
try:
print link
links=link[6:]
linkss="http://www.mrp.com"+links
print linkss
imaglinks=getlastlink(getHtml(linkss))
#print getHtml(links)
print len(imaglinks)
print imaglinks
except Exception:
pass
for imaglink in imaglinks:
try:
print imaglink
imagname='aa'+str(i)+'.jpg'
getAndSaveImg(imaglink,imagname)
except Exception:
pass
finally:
i+=1
print j
j+=1
所有的东西还是要用起来,如果不用的话, 做起来和学起来一点动力都没有,也觉得没有深究的意义,还是要写一写高级的东西的,不然一直都是这么low。
相关文章推荐
- python“不为人知的”特性
- numpy使用过程中小函数记录
- python学习笔记(pict+subprocess)
- python技巧
- 树莓派+python 远程点亮LED灯
- 关于LoadRunner压测过程中出现的error26601的解释
- 我的博客
- python中的数据包处理模块scapy调研笔记
- 整理python教程
- python 画图
- adaboost python 实例
- 通过euclidean_distances计算向量之间的距离
- 数据库里有数据但是python取出来发现是空的
- 用快速使用Python写一个拼写检查器
- 最经典25本Python编程开发电子书精粹
- 解决mac-osx10.11下无法安装wxPython2.8-osx-unicode-2.8.12.1的问题
- Python3.5入门学习记录-模块
- Python进行数据科学工作的简单入门教程
- python cmd
- python模块:re模块