代码如下:
from sgmllib import SGMLParser
import urllib2
class sgm(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.srcs=[]
self.ISTRUE=True
def start_div(self,artts):
for k,v in artts:
if v=="author":
self.ISTRUE=False
def end_div(self):
self.ISTRUE=True
def start_img(self,artts):
for k,v in artts:
if k=="src" and self.ISTRUE==True:
self.srcs.append(v)
def download(self):
for src in self.srcs:
f=open(src[-12:],"wb")
print src
img=urllib2.urlopen(src)
f.write(img.read())
f.close()
sgm=sgm()
for page in range(1,500):
url="http://www.qiushibaike.com/late/page/%s?s=4622726" % page
data=urllib2.urlopen(url).read()
sgm.feed(data)
sgm.download()
本文介绍了Python操作MYSQL、执行SQL语句、获取结果集、遍历结果集、取得某个字段、获取表字段名、将图片插入数据库、执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐。如果想查看在线版请访问:https://www.jb51.net/article/34102.htm
0
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号