python学习的第五天

1、图片的下载

例如:
代码块:

# 图片的下载
import requests
url = 'http://g.hiphotos.baidu.com/image/pic/item/5366d0160924ab18014cefd83bfae6cd7a890b82.jpg'
response = requests.get(url)
# 获取bytes类型的响应
data = response.content
with open('风景.jpg', 'wb') as f:
    f.write(data)
风景.jpg

2、豆瓣TOP250爬虫

代码块:

import requests
from lxml import etree
def spider_douban_top250():
    movie_list_info = []
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
    # url = 'https://movie.douban.com/top250'
    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 = 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.xpath('./div[@class="item"]/div[@class="info"]/div[@class="hd"]/a/span[1]/text()')[0]
            # print(movie_name)

            # 电影的介绍
            movie_introduce = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[1]/text()')[0].strip()
            # print(movie_introduce)

            # 电影的星级
            star = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[2]/text()')[0]
            # print(star)

            # 电影的评价
            evalute = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')
            evalute = evalute[0].replace('人评价', '')
            # print(evalute)

            # 电影的描述
            describe = movie.xpath('./div[@class="item"]/div[@class="info"]/div[@class="bd"]/p[@class="quote"]/span[1]/text()')
            # print(describe)

            # 电影的封面
            movie_img_url = movie.xpath('./div[@class="item"]/div[@class="pic"]/a/img/@src')[0]
            # print(movie_img_url)

            # 添加每一个商家的图书信息
            movie_list_info.append({
                'serial_number': serial_number,
                'movie_name': movie_name,
                'movie_introduce': movie_introduce,
                'star': star,
                'evalute':evalute,
                'describe':describe,
                'movie_img_url':movie_img_url
            })

    for movie in movie_list_info:
        print(movie)

    # 下载图片
    for movie in movie_list_info:
        url = movie['movie_img_url']
        resp = requests.get(url)
        if resp.status_code == 200:
            img_name = '0000000{}.jpg'.format(movie['serial_number'])
            # data = resp.content
            with open('./imgs/{}'.format(img_name), 'wb') as f:
                f.write(resp.content)

spider_douban_top250()
image.png
imgs.png

注:headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}来自于页面右键(不同浏览器不同选项名)
image.png
image.png

注解:
①随意一个js里面会在Headers页尾底有,如果没有看见js文件则刷新页面再看;

②XPath里面的内容可以在下图所示检验(例如影片序号)

③上述②中
是在Google Chrome浏览器中的网上应用商店安装的XPath Helper

你可能感兴趣的:(python学习的第五天)