1、图片的下载
# 图片的下载
import requests
url = 'http://www.hinews.cn/pic/003/005/430/00300543000_b6551f10.jpg'
response = requests.get(url)
# 获取bytes类型的响应
data = response.content
with open('lixian.jpg', 'wb') as f:
f.write(data)
结果:
李现
2、豆瓣TOP250电影排行爬虫
- 解决 “from lxml import etree”导入时etree标红问题:
from lxml import html
etree = html.etree
- 爬虫爬排行榜信息并下载电影封面
import requests
from lxml import html
etree = html.etree
def spider_douban_top250():
movie_list_info = []
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
# 根据url地址栏的页面start=?规律设置步长
for i in range(0, 250, 25):
url = 'https://movie.douban.com/top250?start={}&filter='.format(i)
# 获取bytes类型响应
data = requests.get(url, headers=headers).content
html = etree.HTML(data)
ol_list = html.xpath('//div[@id="content"]//div[@class="article"]/ol/li')
for movie in ol_list:
# 影片序号
# serial_number
serial_number = movie.xpath('./div[@class="item"]/div[@class="pic"]/em/text()')
if len(serial_number) == 0:
serial_number = ''
else:
serial_number = serial_number[0]
# print(serial_number)
# 电影的名字
# movie_name
movie_name = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="hd"]/a/span[1]/text()')[0]
# print(movie_name)
# 电影的介绍
# movie_intro
movie_intro = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[1]/text()')[0].strip()
# print(movie_intro)
# 电影的星级
# star
star = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[2]/text()')[0]
# print(star)
# 电影的评价
# evaluate
evaluate = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')
evaluate = evaluate[0].replace('人评价', '')
# print(evaluate)
# 电影的描述
# describe
describe = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[@class="quote"]/span[1]/text()')
# print(describe)
# 电影封面地址
# movie_img_path
movie_img_path = movie.xpath('./div[@class="item"]/div[@class="pic"]/a/img/@src')[0]
# print(movie_img_path)
movie_list_info.append({
'serial_number': serial_number,
'movie_name': movie_name,
'movie_intro': movie_intro,
'star': star,
'evaluate': evaluate,
'describe': describe,
'movie_img_path': movie_img_path
})
for movie in movie_list_info:
print(movie)
# 下载图片
for movie in movie_list_info:
# 获取图片地址
url = movie['movie_img_path']
resp = requests.get(url)
# 如果请求成功
if resp.status_code == 200:
# 图片文件名字以排行序号命名
img_name = '0000000{}.jpg'.format(movie['serial_number'])
with open('./imgs/{}'.format(img_name), 'wb') as f:
f.write(resp.content)
spider_douban_top250()
结果:
电影封面图片