主要是是在学习过程中的一些记录笔记
有一部分是莫凡视频教程里的代码,正则部分是别的视频里的
# print 函数
print("hello world")
print('hello world'+str(4))#转换为字符串
print("int(2**2)")
print(2**2) # 2的2次方
print(2+2)# 2+2
print(int('2')+2)# 字符串转换为整型
# 运算符
print(8%2)# 取余数
print(8//2)# 取整 4 有多少个2
# 自变量
apple_one=1+20
print(apple_one)#
#定义多个变量
a,b,c=1,2,3
print(a,b,c)
# while 循环
condition=1
while condition<10:
print(condition)
condition=condition+1
# for 循环
example_list=[1,2,3,4,5,6,7,8,900]
for i in example_list:
print(i)# 输出 数组里的每一个数
for i in range(1,17,3):# 不包括 17
print(i)
#if else
x,y,z=1,2,3
if x>y:
print("x>y")
elif x>1: #只要有一个成立,后面的就不会执行
print("")
else:
print("x<y")
#def 定义函数
def function(a=1,b=1,c=1):# 定义这个而函数,当调用时候才会执行;可以给定默认值
c=a+b
print("the c is=",c,
"the b is=",b,
"the a is=",a)
function(2,3,4)
# 全局变量 局部变量
APPLE=100#全局变量一般都大写,在函数外面
a1=None
def fun():
global a1# 定义全局变量
a1=10
print(a1)
return a1+100
#fun()#执行函数
print(fun())#执行函数,并输出返回值
# 安装模块
# pip3 install
# pip3 uninstall numpy
# pip3 install numpy-1.9.2
#pip3 install -U numpy
# 文件读写
text=" this is .\n test file .\n this is next line .\n"#
print(text)
my_file=open("my_file.txt","w")# “w”写 “r”读
my_file.write(text)
my_file.close()
#在文件后追加文字
text="wo\na\n"
my_file=open("my_file.txt","a")
my_file.write(text)
my_file.close()
file=open("my_file.txt","r")# 打开是open
# content=file.read()
# print(content)
content2=file.readlines()
print(content2)
# 定义类
class Calculator:
name="Good Calculator" # 属性
price="18"#功能
def add(self,x,y):#self 说明在类是默认参数,会被传下去
result=x+y
print(self.name)
print(result)
ca=Calculator()
print(ca.name)
print(ca.price)#外部调用 定义的类名,内部用的话用self
ca.add(5,6)
#init功能 初始
class Calculator:
# name="Good Calculator" # 属性
# price="18"#功能
def __init__(self,name,price,hight,width,weight):
self.name=name # 我感觉就是默认参数
self.price=18
self.h=hight
self.wi=width
self.wi=weight
def add(self,x,y):#self 说明在类是默认参数,会被传下去
result=x+y
print(self.name)
print(result)
aa=Calculator("BAD",12,30,15,10)
print(aa.name)
print(aa.wi)#外部调用 定义的类名,内部用的话用self
#input
# a_input=input("爱不爱我:")
# print("输入的是",a_input)
# if a_input=="爱":
# print("哈哈哈")
# else:
# print("滚吧")
# 元组和列表
#tuple lise
a_tuple=(12,12,13,14)#我觉得元组就是不可变的列表
a_list=[12,13,14,15]
print(a_list)
for content in a_list:
print(content)
for index in range(len(a_list)):#index被赋值了0-len(a_list) 之间的数
print("index=",index,"number in list",a_list[index])
# 列表
a=[1,2,3,4,5,6]
a.append(21)# 还可以remove
print(a)
a.insert(1,23)#列表的第一位是里面的第二个
print(a)
print(a[-1])#打印出最后一位
print(a[0:3])#打印出最后一位
print(a.index(2))#第一个等于2 他的索引 第一次出现2的索引
print(a.count(3))#出现3的次数
a.sort(reverse=True)#排序 reverse=True不写的时候按从小到大排序
print(a)
# 多为列表 numpy pandas 两个模块
a=[1,2,3,4,5]# 一行5列
multi_dim_a=[
[1,2,3],
[4,5,6],
[7,8,9]]#3行3列
print(a[1])
print(multi_dim_a[2][2])
#字典 {}
d={"apple":1,"pear":2,"orange":3}
print(d["apple"])
del d["pear"]# 减去
print(d)
d["b"]=20#加进去
#加载模块
import time as t #把time替换成t
# print(t.localtime())
# t.time()
from time import time,localtime # 或者*
print(time())
print(localtime())
#创建自己的模块 脚本
# 在m1.py 里写入一下内容
# def printdata(data):
# print(data)
import m1
m1.printdata("哈哈哈哈\n")
#contine break
# while True:
# b=input("type something")
# if b=="1":
# break #a=False
# else:
# pass# pass 什么都不做
# print("en ?")
# try 处理错误
try:
file=open("eeee.txt","r+")
except Exception as e:
print(e)
response=input("是否想创建文件")
if response=="y":
file=open("eeee.txt","w")
else:
pass
else:
file.write("ssss")
file.close()
#zip lambda map
a=[1,2,3]
b=[4,5,6]
print(list(zip(a,b)))#输出 [(1, 4), (2, 5), (3, 6)]
for i,j in zip(a,b):
print(i/2,j*2)
def fun1(x,y):
return(x+y)
fun2=lambda x,y:x+y
fun2(2,3)#输出5
map(fun1,[1],[2])
list(map(fun1,[1],[2]))#输出[3]
import copy
a=[1,2,3]
b=a #改变a则b也跟着变
c=copy.copy(a) #浅拷贝
print(id(a)==id(c)) #false
c=copy.deepcopy(a) #深拷贝 任何东西都不重复
# threading 多线程
#元字符 [] ^ $
import re
st="top tip twp"
res=r"t[iow]p"#规则
print(re.findall(res,st))
s="hello,losd,jlfj"
#s="hel,hellolosd,jlfj"
r="^hello"# s的行首是hello时候才管用
print(re.findall(r,s))
r="jlfj$"#行位$
print(re.findall(r,s))
r=r"\^ab" #转义字符,取消元字符
s="^abc"
print(re.findall(r,s))
# \d 相当于[0-9] \D 相当于[^0-9] \s 匹配任何空白字符 \S 匹配任何非空白字符 \w 匹配任何字母数字字符 \W 匹配任何非字符字符
r=r"s[d]"
print(re.findall(r,"s sdf ddd ss "))
r=r"^010-?\d{8}$" #^010 表示010开头 $表示八个数字结尾 ? 表示-可有可无 + 做大匹配 +? 最小匹配
print(re.findall(r,"010-12345678"))#['010-12345678'] \d表示数字,{8}表示前面的规则重复8次
r=r"ab*"
print(re.findall(r,"abbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))# *表示重复很多次 + 至少一次 ? 可有可无
r=r"010-?\d{8}" # +? {3,4}
r1=r"\d{3,4}-?\d{8}"
print(re.findall(r1,"01012345678"))
p_tel=re.compile(r1)
p_tel.findall("01012345678")#经常用的话,可以把正则给编译了,然后整成一个对象,速度变快
print(p_tel.findall("01012345678"))
# match search findall finditer
help(re.sub)
# match在开头找 search 在整个里面找
csvt_re=re.compile(r"csVt",re.I)# re.I 不区分大小写
csvt_re.findall("csvt")
print(csvt_re.findall("csvt"))
csvt_re.match("csvt hello")
print(csvt_re.match("csvt"))
csvt_re.search("csvt hello a ")
print(csvt_re.search("csvt hello a "))
rs=r"c..t"
re.sub(rs,"python","c1svt cddt sggt")#sub 把c..t 的都替换成python subn 可以加替换次数
print(re.sub(rs,"python","csvt cddt sggt"))
ip="1.2.3.4"
ip.split(".")
print(ip.split("."))
s="15313+4545*"
re.split(r'[\+\-\*]',s)
print(re.split(r'[\+\-\*]',s)) #['15313', '4545', '']
#标志符号
r=r"csvt.net"
re.findall(r,"csvt.net",re.S)
print(re.findall(r,"csvt\nnet",re.S)) # . 就可以匹配包括换行在内的所有字符
s="""
hello csvt
csvt hello
hello csvt hello
csvt hhe
"""
r=r"^csvt"
print(s)
print(re.findall(r,s,re.M)) #re.M 多行
#如果正则被写成多行,如果print tel会发现,他里面有很多\n,所以应该re.X
tel=r"""
\d{3,4}
-?
\d{8}
"""
re.findall(tel,"010-12345678")
print(re.findall(tel,"010-12345678",re.X))
#分组
email=r"\w{3}@w+(\.com|\.cn"
s="""
jslkfjl hello src=scvt yes jsjdfl
skjdfklsjflsfl src=123 yes
ser=123 yes
"""
print(s)
r=r"hello src=.+ yes" # . 除了换行符之外的任意字符 + 重复一次或一次以上
print(re.findall(r,s)) #['hello src=scvt yes']
#用()分组 (.+)
r=r"hello src=(.+) yes"
print(re.findall(r,s)) #['scvt'] 只输出等号后的内容
import re
import urllib.request
def gethtml(url):
page=urllib.request.urlopen(url)#打开页面
html=page.read()# 把数据读取出来
return html#返回
# 匹配结果是:src="test.jpg" width="60px" height="80px"
# 意思是从="往后匹配,直到最后一个"匹配结束
# 懒惰模式正则:
# src=".*?"
# 结果:src="test.jpg"
# 因为匹配到第一个"就结束了一次匹配。不会继续向后匹配。因为他懒惰嘛。
def getImg(html):
reg = r'src="(.*?\.jpg)" size'# \ 转义字符 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。
imgre=re.compile(reg)
imglist=re.findall(imgre,html.decode('utf-8'))
print(imglist)
x=0
for i in imglist:
urllib.request.urlretrieve(i,'%s.jpg' %x)# python的打印格式 带%
x=x+1
html=gethtml('https://tieba.baidu.com/p/5817279454')
print(getImg(html))
#
#
# s='gfhg gfhfhf src="https://imgsa.baidu.com/forum/w%3D580/sign=4b8f3d0ccfcec3fd8b3ea77de68ad4b6/6aed2e738bd4b31c759c547b8bd6277f9f2ff84b.jpg" size=51516'
# r = r'src="(.*?\.jpg)" size'
# re.findall(r,s)
# print(re.findall(r,s))
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
#下面这个可以用了
import urllib
from bs4 import BeautifulSoup
#beautifulsoup方法,第三方库的方法,爬找网页
## 下载网页
def get_content(url):
html = urllib.request.urlopen(url)
content = html.read().decode('utf-8')#转码
html.close()#记得要将打开的网页关闭,否则会出现意想不到的问题
print (type(content))
return content
def get_image(info):
#print("info=",info)
reg = r'img src="(.*?\.jpg)'
all_image = re.findall(reg,info)
print("all_image=","\n",all_image)
x=1
for image in all_image:
print(all_image)
try:
urllib.request.urlretrieve(image,'%s.jpg' % x)
x+=1
except Exception as e:
continue
url = "https://www.zhihu.com/question/299205851/answer/571602599"
info = get_content(url)
get_image(info)
#可以下载60张/////////////////
# -*- coding:utf-8 -*-
import re
import requests
def dowmloadPic(html, keyword):
pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
i = 1
print('找到关键词:' + keyword + '的图片,现在开始下载图片...')
for each in pic_url:
print('正在下载第' + str(i) + '张图片,图片地址:' + str(each))
try:
pic = requests.get(each, timeout=10)
except requests.exceptions.ConnectionError:
print('【错误】当前图片无法下载')
continue
dir = 'picture/' + keyword + '_' + str(i) + '.jpg'
fp = open(dir, 'wb')
fp.write(pic.content)
fp.close()
i += 1
if __name__ == '__main__':
word = input("Input key word: ")
url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201326592&v=flip'
result = requests.get(url)
dowmloadPic(result.text, word)
# 可以下载240张图片
import requests
import re
import os
def get_page_url(url, param):
response = requests.get(url, params=param)
response.encoding = 'utf-8'
return response.text
def parse_page(str):
pattern = re.compile('"middleURL":"(.*?)",')#利用正则匹配图片url
url_list = re.findall(pattern, str)
return url_list
def run(keyword, path):
url = "https://image.baidu.com/search/acjson"
i = 0
for j in range(30, 270, 30):
params = {"ipn": "rj", "tn": "resultjson_com", "word": keyword, "pn": str(j)}
html = get_page_url(url, params)
lists = parse_page(html)
print(lists)
for item in lists:
try:
img_data = requests.get(item, timeout=10).content
with open(path + "/" + str(i) + ".jpg", "wb") as f:
f.write(img_data)
f.close()
i = i+1
except requests.exceptions.ConnectionError:
print('can not download')
continue
def make_dir(keyword):
path = "D:/百度图片/"
path = path+keyword
is_exists = os.path.exists(path)
if not is_exists:
os.makedirs(path)
return path
else:
print(path + '目录已存在')
return path
def main():
keyword = input("input keyword about images you want to download: ")
path = make_dir(keyword)
run(keyword, path)
if __name__ == '__main__':
main()
#、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、可以无限下载
#https://blog.youkuaiyun.com/Geek_/article/details/89928690
from urllib.parse import urlencode
import requests
import re
import os
# 图片下载的存储文件
save_dir = '百度图片/'
# 百度加密算法
def baidtu_uncomplie(url):
res = ''
c = ['_z2C$q', '_z&e3B', 'AzdH3F']
d = {'w': 'a', 'k': 'b', 'v': 'c', '1': 'd', 'j': 'e', 'u': 'f', '2': 'g', 'i': 'h', 't': 'i', '3': 'j', 'h': 'k',
's': 'l', '4': 'm', 'g': 'n', '5': 'o', 'r': 'p', 'q': 'q', '6': 'r', 'f': 's', 'p': 't', '7': 'u', 'e': 'v',
'o': 'w', '8': '1', 'd': '2', 'n': '3', '9': '4', 'c': '5', 'm': '6', '0': '7', 'b': '8', 'l': '9', 'a': '0',
'_z2C$q': ':', '_z&e3B': '.', 'AzdH3F': '/'}
if (url == None or 'http' in url): # 判断地址是否有http
return url
else:
j = url
# 解码百度加密算法
for m in c:
j = j.replace(m, d[m])
for char in j:
if re.match('^[a-w\d]+$', char): # 正则表达式
char = d[char]
res = res + char
return res
# 获取页面信息
def get_page(offset):
params = {
'tn': 'resultjson_com',
'ipn': 'rj',
'ct': '201326592',
'is': '',
'fp': 'result',
'queryWord': '美女', # 关键字
'cl': '2',
'lm': '-1',
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': '-1',
'z': '',
'ic': '0',
'word': '哈士奇', # 关键字
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': '0',
'istype': '2',
'qc': '',
'nc': '1',
'fr': '',
'expermode': '',
'pn': offset * 30,
'rn': '30',
'gsm': '1e',
'1537355234668': '',
}
url = 'https://image.baidu.com/search/acjson?' + urlencode(params)
try: # 尝试连接服务器
response = requests.get(url)
if response.status_code == 200: # 获取HTTP状态,即服务器响应HTTP请求
return response.json()
except requests.ConnectionError as d:
print('Error', d.args)
# 获取图像
def get_images(json):
if json.get('data'):
for item in json.get('data'): # 获取图片数据字典值
if item.get('fromPageTitle'): # 获取图片Title
title = item.get('fromPageTitle')
else:
title = 'noTitle'
image = baidtu_uncomplie(item.get('objURL')) # 图片地址
if (image):
yield { # 存储图片信息
'image': image,
'title': title
}
def save_image(item, count):
try:
response = requests.get(item.get('image'))
if response.status_code == 200: # 获取HTTP状态,即服务器响应HTTP请求
file_path = save_dir + '{0}.{1}'.format(str(count), 'jpg') # 命名并存储图片
if not os.path.exists(file_path): # 判断图片是否在文件中
with open(file_path, 'wb') as f:
f.write(response.content)
else:
print('Already Downloaded', file_path)
except requests.ConnectionError: # 如果出现连接错误
print('Failed to Save Image')
def main(pageIndex, count):
json = get_page(pageIndex)
for image in get_images(json):
save_image(image, count)
count += 1
return count
if __name__ == '__main__':
if not os.path.exists(save_dir): # 判断是否存在文件,若没有则创建一个
os.mkdir(save_dir)
count = 1
for i in range(1, 200): # 循环页数下载图片
count = main(i, count) # i表示页数,统计图片并运行主函数
print('total:', count)