-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path爬取网站图片.py
67 lines (53 loc) · 2.24 KB
/
爬取网站图片.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env Python3
# -*- encoding:utf-8 *-*
'''@author = 'Appleyk' '''
'''@time = '2017年9月23日11:42:32' '''
import re # 使用正则表达式
from urllib import request
def getResponse(url):
# url请求对象 Request是一个类
url_request = request.Request(url)
# print("Request对象的方法是:",url_request.get_method())
# 上下文使用的对象,包含一系列方法
# url_response = request.urlopen(url) #打开一个url或者一个Request对象
url_response = request.urlopen(url_request)
'''
geturl():返回 full_url地址
info(): 返回页面的元(Html的meta标签)信息
<meta>:可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词。
getcode(): 返回响应的HTTP状态代码
100-199 用于指定客户端应相应的某些动作。
200-299 用于表示请求成功。 ------> 200
300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息。
400-499 用于指出客户端的错误。 ------> 404
500-599 用于支持服务器错误。
read(): 读取网页内容,注意解码方式(避免中文和utf-8之间转化出现乱码)
'''
return url_response # 返回这个对象
def getJpg(data):
jpglist = re.findall(r'src="http.+?.jpg"', data)
return jpglist
def downLoad(jpgUrl, n):
# request.urlretrieve(jpg_link, path)
try:
request.urlretrieve(jpgUrl, '%s.jpg' % n)
except Exception as e:
print(e)
finally:
print('图片%s下载操作完成' % n)
http_response = getResponse("https://www.speedtest.cn/") # 拿到http请求后的上下文对象(HTTPResponse object)
# print(http_response.read().decode('utf-8'))
# print(http_response.geturl()) # 打印这个对象的url
# print(http_response.info()) # 打印这个对象的info
# print(http_response.getcode()) # 打印这个对象的状态码
# print(http_response.read()()) # 打印这个对象的源码
data = http_response.read().decode('utf-8')
# print(data)
global n
n = 1
L = getJpg(data)
for jpginfo in L:
print(jpginfo)
s = re.findall(r'http.+?.jpg', jpginfo)
downLoad(s[0], n)
n = n + 1