zoukankan      html  css  js  c++  java
  • python爬虫之下载京东页面图片

    import requests
    from bs4 import BeautifulSoup
    import time
    import re
    t = 0   #用于给图片命名
    for i in range(10):
        url = "https://list.jd.com/list.html?cat=9987,653,655&page=%s&sort=sort_rank_asc&trans=1&JL=6_0_0&ms=6#J_main"%i #起始url
        #设置header,模拟浏览器发出请求
        header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
        }
        data = requests.get(url,headers=header).content 
        soup = BeautifulSoup(data,'lxml')#使用beautifulsoup解析上面获得的html为文档
        img_link  = soup.select('div#plist div.p-img')#先缩小目标数据所在的范围
        link_list = re.findall('//img[0-9|.|a-z|/|A-Z]+',str(img_link))#在上面的范围中使用正则找到对应的img src
    
        #遍历img src 下载到本地
        for url in link_list:
            url = 'https:'+str(url)
            data = requests.get(url).content
            filename = "C:/Users/Administrator/Desktop/京东图片/" + str(t) +".jpg"
            t += 1
            with open(filename,mode='wb') as f:
                f.write(data)
  • 相关阅读:
    CF1137C Museums Tour(tarjan+DP)
    Educational Codeforces Round 65 (Rated for Div. 2)
    Codeforces Round #559(Div.1)
    委托
    类库
    is 和 as 运算符
    面向对象 接口
    抽象类
    面向对象 多态
    访问修饰符 程序集 静态方法
  • 原文地址:https://www.cnblogs.com/xiaohuyi/p/10347039.html
Copyright © 2011-2022 走看看