一、爬取一张图片
import requests
url = "地址"
resp = requests.get(url)
print(resp.content)
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
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()