• 爬取图片


    pycharm可以用ctrl+alt+l对代码进行自动格式规范化。

    #!author zhang
    import requests #引入requests包#爬取网站图片
    import os
    from bs4 import BeautifulSoup
    r=requests.get("http://699pic.com/sousuo-218808-13-1.html") #请求网站
    fengjing=r.content # 以html的格式返回
    #print(s)
    soup=BeautifulSoup(fengjing,"html.parser")
    imags=soup.find_all(class_="lazy") #使用firbug查看属性
    lujing="C:\Users\Administrator\Desktop\python3\test_blog\test_case\twodir\threedir\jpg" #定义获取图片存储的路径
    print(imags) #返回list对象
    for i in imags:
    title = i["title"]
    jpg_url=i["data-original"]
    print(title)
    print(jpg_url)
    print(" ")
    with open(lujing+title+'.jpg',"wb") as f: #equests里get打开图片的url地址,content方法返回的是二进制流文件,可以直接写到本地
    f.write(requests.get(jpg_url).content)

  • 相关阅读:
    Best Time to Buy and Sell Stock II
    Subsets II
    Subsets I
    Combinations
    Permutation Sequence
    Next Permutation
    Anagrams
    Combination-Sum II
    Combination-Sum I
    Permutations II
  • 原文地址:https://www.cnblogs.com/x2x3/p/7448369.html
Copyright © 2020-2023  润新知