豆瓣实战1

豆瓣实战(1)

import requests
import bs4
import json
import datetime
import os
import sys
import urllib

#建立文件夹函数
def mkdir(path):
    path=path.strip()					#去掉文件夹名两边的空格	
    isExists=os.path.exists(path)		#判断文件夹是否存在
    if not isExists:
        os.makedirs(path)				#不存在则创建
        print("successful")
    else:
        print("content is exits,needn't download!")
        sys.exit(0)
        
#保存图片
def saveImage(url,name):				
    img=requests.get(url)				#请求图片链接
    file_name=name+".jpg"				#文件名
    f=open(file_name,"ab")				#打开文件
    f.write(img.content)				#写入文件
    f.close()
    
def set(**d):		·					#判断图片是否加载完毕
    for i in d:							#分析最后一次json请求返回的headers,找到差异作为判断条件
        if i=="Content-Length":
            return True
    
    return False    

#下载
def download():
	with open("d:\\python_study\\BeautifulPicture\\movie.txt","a",encoding="utf8") as f:#打开文件,以追加的方式
        for i in json:
            #saveImage(i['cover_url'],i['title'])					#保存图片
            for j in i.values():									#遍历字典的values
                if isinstance(j,list):								#判断values的类型
                    f.write(("|".join(j))+",")
                    
                elif isinstance(j,int):
                    f.write(str(j)+",")
                    
                else :
                    f.write(str(j)+",")
                    
            f.write("\n")

os.chdir("d:\python_study\BeautifulPicture")						#切换到父目录
path=datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")			#生成文件夹名,以系统时间为文件夹名
mkdir(path)															#生成文件夹
os.chdir("d:\\python_study\\BeautifulPicture\\"+path)				#切换到目录

headers={
    'Host': 'movie.douban.com',
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
    'Referer': 'https://movie.douban.com/typerank?type_name=%E5%89%A7%E6%83%85%E7%89%87&type=11&interval_id=100:90&action='

}

referer="https://movie.douban.com"
url="https://movie.douban.com/j/chart/top_list?type=11&interval_id=100:90&action=&start=600&limit=20"


#print(type(json))


i=0

while True:
    url="https://movie.douban.com/j/chart/top_list?type=11&interval_id=100:90&action=&start="+str(i)+"&limit=20"
    response=requests.get(url,headers=headers)
    json=response.json()
    d=response.headers
    if set(**d):
        download()
        break
    else:
        download()
        
    i=i+20

分析豆瓣页面

  • 页面是动态获取,分析可知加载新图片是发出一个请求,返回json数据,json里含有加载的信息
  • 分析请求链接,寻找规律。action=&start=0&limit=20
  • 模拟请求,接受json数据。
  • 具体代码中有注释
  • 最后说一下,一定要做好应对反爬虫的措施,ip已封
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值