本人之前对python也有一些了解,最近工作不是很忙,故又研究下python。
前提准备:python、BeautifulSoup(需要部署包的朋友私信我)
import urllib2
import urllib
import re
from bs4 import BeautifulSoup
def getHtml(url):
return urllib2.urlopen(url).read()
url='https://movie.douban.com/cinema/nowplaying/chengdu/'
html=getHtml(url);
soup=BeautifulSoup(html,"html.parser")
lists=soup.findAll(class_='list-item')
try:
for list in lists:
t= list.attrs['data-title']
img_src= list.find(src=re.compile('https.*?jpg')).attrs['src']
image_name = "D:\python\i20180514\%s.jpg" % t;
urllib.urlretrieve(img_src, image_name)
print '***********************'
except:
print "Error: 没有相关属性"
else:
print "查询成功"
这样一个简单的案例就成功啦。
每天积累一点,收获就会多一点。