python爬虫下载图片--艺术网站

本文介绍了一种使用Python和urllib2库抓取特定网站上艺术家作品的方法,包括连接MySQL数据库、设置请求头、解析HTML、抓取图片并保存到本地的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import MySQLdb
import MySQLdb.cursors
import urllib2
import urllib
import json
import re
import time
import os
import random
from bs4 import BeautifulSoup
db = MySQLdb.connect(host='localhost', user='root', passwd='root', db='python', port=3306, charset='utf8', unix_socket='/tmp/mysql.sock',cursorclass = MySQLdb.cursors.DictCursor)
db.autocommit(True)
cursor = db.cursor()
#title='name;phone;address;contect;e-maill;email;QQ;tel;url;\n'
#fw.write(title.encode('gbk'))
headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
url="http://zengfanzhi.artron.net/works"
request = urllib2.Request(url=url,headers=headers)		
response = urllib2.urlopen(request)
html = response.read()
html = BeautifulSoup(html,"html5lib",from_encoding='utf-8')
strj=str(html)
#strj='[{"imageSrc":"http://img4.artron.net/artist/A0000077/brt000007700049.jpg"}];'
strjson=re.findall(r"\[\{(.+?)\}\]",strj)
strjson="[{"+strjson[0]+"}]"
#in_json = eval(strjson)
#print type(in_json)
imgjson=json.loads(strjson)
author=html.find('title').get_text()
path='/mnt/study/bailian/'+author
if not os.path.exists(path):
	os.makedirs(path)
for i in range(len(imgjson)):
	imgsrc=imgjson[i]['imageSrc']
	title=imgjson[i]['title']
	urllib.urlretrieve(imgsrc,'%s/%s.jpg' % (path,title))
	print imgsrc

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值