python爬取图片

一、爬取一张图片

#图片存储入门
import requests
#数据爬取
url = "地址"
resp = requests.get(url)
print(resp.content)#图片用content,其他用text

#存储图片
with open("image/01.jpg",'wb') as f:#图片要用二进制形式写入
    f.write(resp.content)

二、爬取一页图片

import requests
import re
#获取
url = "地址"
response = requests.get(url)
html_str = response.text

pattern = re.compile('正则表达')
urls = re.findall(pattern,html_str)
print(urls)
for u in urls:
    res = requests.get(u)
    #存储图片
    name = u.split("/")[-1]
    with open("image/"+name,'wb') as f:
        f.write(res.content)
    print("下载"+name+"成功")

三、爬取整个网站图片(封装写法)

import  requests
import re
import threading

#获取urls
def get_html(page):
    urls = []
    for i in range(page):
        url = "地址"
        response = requests.get(url)
        html_str = response.text
        pre = re.compile('正则表达式')
        rre = re.findall(pre,html_str)
        urls+=rre
    return urls

#下载图片
def download_image(url):
    res = requests.get(url)
    name = url.split("/")[-1]
    with open("image/"+name,'wb') as f:
        f.write(res.content)
    print("下载"+name+"成功")

if __name__ == '__main__':
    page = int(input("scrapy page:"))
    urls = get_html(page)
    #加入线程,提高效率
    for url in urls:
        t = threading.Thread(target=download_image,args=(url,))
        t.start()

你可能感兴趣的:(python小脚本,爬虫)