diff --git a/README.md b/README.md index 7691d46..62f4376 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ +<<<<<<< HEAD # python_study python 个人学习日记 +======= +# python-study +Config files for my GitHub profile. +2021.8.30 +>>>>>>> be149c204cddaac40ada1bc2652fd22002863a06 diff --git a/alien_invasion/alien_invasion.py b/alien_invasion/alien_invasion.py new file mode 100644 index 0000000..091860e --- /dev/null +++ b/alien_invasion/alien_invasion.py @@ -0,0 +1,26 @@ +import sys + +import pygame + +def run_game(): + #初始化游戏并创建一个屏幕对象 + pygame.inin() + screen = pygame.display.set_mode((1200,800)) + pygamde.dispaly.set_caption('Alien Invasion') + + + # 设置背景颜色 + bg_color = (230,230,230) + + # 开始游戏的主循环 + while True: + + #监视鼠标键盘事件 + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + + # 让最近绘制的屏幕可见 + pygame.display.flip() + diff --git a/function/BMI.py b/function/BMI.py new file mode 100644 index 0000000..7f14841 --- /dev/null +++ b/function/BMI.py @@ -0,0 +1,33 @@ + + + +height = float(input('请输入你的身高。单位是m,保留两位小数:')) + +weight = float(input('请输入您的体重。单位是kg,保留两位小数:')) + +BMI_index_number = weight / (height**2) + +if BMI_index_number <= 18.5: + + print('过轻') + +elif 18.5 < BMI_index_number <= 25: + + print('正常') + +elif 25 < BMI_index_number <= 28: + + print('过重') + +elif 28 < BMI_index_number <= 32: + + print ('肥胖') + +else: + print('严重肥胖') + +print(BMI_index_number) + + + + diff --git a/function/__pycache__/BMI.cpython-39.pyc b/function/__pycache__/BMI.cpython-39.pyc new file mode 100644 index 0000000..b1faa48 Binary files /dev/null and b/function/__pycache__/BMI.cpython-39.pyc differ diff --git a/function/__pycache__/quadratic.cpython-39.pyc b/function/__pycache__/quadratic.cpython-39.pyc new file mode 100644 index 0000000..cb776cc Binary files /dev/null and b/function/__pycache__/quadratic.cpython-39.pyc differ diff --git a/function/quadratic.py b/function/quadratic.py new file mode 100644 index 0000000..70c02ca --- /dev/null +++ b/function/quadratic.py @@ -0,0 +1,35 @@ + +import math + +def my_quadratic(a, b, c): + + if not isinstance(a,(int,float)) or not isinstance(b,(int,float)) or not isinstance(c,(int,float)): + + raise TypeError('bad operand type') + + d = b**2 - 4*a*c # 数字和字母相乘需要‘ * ’,和数学还是有差别的 + + if a == 0: # ‘ == ’在Python中是等于而‘ = ’是赋值,已踩坑 + + print('该方程没有意义') + + elif d < 0: # 小于零后是负数式子就不成立,所以无解 + + print('该方程无解') + + elif d == 0: + + x = -b/(2*a) # + + print(f'该方程的两个解为:x1=x2={x:.1f}') + + else: + + x1 = (-b+math.sqrt(d))/(2*a) + + x2 = (-b-math.sqrt(d))/(2*a) + + print(f'该方程的两个解为:x1={x1:.1f} x2={x2:.1f}') + + + diff --git "a/python-3.9.6-amd64\345\256\211\350\243\205\345\214\205/python-3.9.6-amd64.exe" "b/python-3.9.6-amd64\345\256\211\350\243\205\345\214\205/python-3.9.6-amd64.exe" new file mode 100644 index 0000000..ae4c7ab Binary files /dev/null and "b/python-3.9.6-amd64\345\256\211\350\243\205\345\214\205/python-3.9.6-amd64.exe" differ diff --git "a/study/notes_python\345\207\275\346\225\260\345\237\272\347\241\200.py" "b/study/notes_python\345\207\275\346\225\260\345\237\272\347\241\200.py" new file mode 100644 index 0000000..831e68c --- /dev/null +++ "b/study/notes_python\345\207\275\346\225\260\345\237\272\347\241\200.py" @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + + +#**************************** 函数 ************************* + +''' +我们知道圆的面积计算公式为: +S = πr2 + +当代码出现有规律的重复的时候,你就需要当心了,每次写3.14 * x * x不仅很麻烦, +而且,如果要把3.14改成3.14159265359的时候,得全部替换。 + +有了函数,我们就不再每次写s = 3.14 * x * x,而是写成更有意义的函数调用s = area_of_circle(x), +而函数area_of_circle本身只需要写一次,就可以多次调用。 + +基本上所有的高级语言都支持函数,Python也不例外。Python不但能非常灵活地定义函数, +而且本身内置了很多有用的函数,可以直接调用。''' + +# abs() 求绝对值函数 max()返回最大值函数 + + +''' +练习 +请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0ax +2 + +bx+c=0 的两个解。 + +提示: + +一元二次方程的求根公式为: + +计算平方根可以调用math.sqrt()函数: + +>>> import math +>>> math.sqrt(2) +1.4142135623730951 +''' + +""" +import math + +def quadratic(a, b, c): + + if not isinstance(a,(int,float)) or not isinstance(b,(int,float)) or not isinstance(c,(int,float)): + + raise TypeError('bad operand type') + + d = b**2 - 4*a*c # 数字和字母相乘需要‘ * ’,和数学还是有差别的 + + if a == 0: # ‘ == ’在Python中是等于而‘ = ’是赋值,已踩坑 + + print('该方程没有意义') + + elif d < 0: # 小于零后是负数式子就不成立,所以无解 + + print('该方程无解') + + elif d == 0: + + x = -b/(2*a) # + + print(f'该方程的两个解为:x1=x2={x:.1f}') + + else: + + x1 = (-b+math.sqrt(d))/(2*a) + + x2 = (-b-math.sqrt(d))/(2*a) + + print(f'该方程的两个解为:x1={x1:.1f} x2={x2:.1f}') + + +""" + + +# 廖雪峰py进度 https://www.liaoxuefeng.com/wiki/1016959663602400/1017261630425888 2021.08.25 + + + +#************************************ 递归函数 *************************************** + +"""card = '''---------------------- 信息卡 ---------------------- +姓名:文逸轩 --- +年龄:12 --- +籍贯:江西 --- +出生日期:2008 --- +------------------------------------------------------''' + +print(card)""" + + + + diff --git "a/study/notes_python\345\237\272\347\241\200.py" "b/study/notes_python\345\237\272\347\241\200.py" new file mode 100644 index 0000000..1c5e411 --- /dev/null +++ "b/study/notes_python\345\237\272\347\241\200.py" @@ -0,0 +1,324 @@ +#!/usr/bin/env pyhon3 +# -*- coding: utf-8 -*- +''' +name = input("Please input you name:") #input 从屏幕输入变量 +print("you name is",name) +''' + +''' +a = 100 +if a >= 0: + print(a) +else: + print(-a) + ''' + +''' +print('I\'m \"OK\"!') #反斜杠 \ 表示转义。 +print("I'm \"OK\"!") +''' + +''' +a = 123 # a是整数 +print(a) +a = 'ABC' # a变为字符串 +print(a) +''' +''' +b= 7//3 #地板除,取除尽整数部分 +print(b) +''' + +"""n = 123 +f = 456.789 +s1 = 'Hello, world' +s2 = 'Hello, \'Adam\'' +s3 = r'Hello, "Bart"' +s4 = r'''Hello, +Lisa!''' +print(n,"\n",f,"\n",s1,"\n",s2,"\n",s3,"\n",s4) +""" + +# 练习 +# 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: + +''' +past_grade = 72 +now_grade = 85 +percentage = (now_grade - past_grade)/past_grade*100 + +print('%s成绩提升了:%.1f%%' % ('小明',percentage)) + +print('{0}成绩提升了:{1:.1f}%'.format('小明',percentage)) # format用法 {0} {1} .format(变量0,变量1) + +print(f'小明成绩提升了:{percentage:.1f}%') 以f开头的字符串,字符串如果包含{xxx},就会以对应的变量替换 +''' + +# 廖雪峰py进度 https://www.liaoxuefeng.com/wiki/1016959663602400/1017092876846880 2021.08.23 + +# 变量 list 和 元组 tuple +''' +classmates = ["wen","liu","wang"] # list (列表) 变量 = [变量0,变量1,变量2,...] 变量类型可以不同 + +classmates[0] # 取第一个变量 "wen" +classmates[-1] # 取最后一个变量 "wang" + +classmates.append("zhao") # 在列表最后 添加变量 "zhao" 变量.append = [变量n,变量n+1,变量n+2,...] +classmates.insert(1, "xue") # 在列表索引 [1] 的位置插入 "xue" 变量.insert = [索引序号,插入的变量] + +classmates.pop() # 在列表删除最后一个索引 变量.pop() +classmates.pop(1) # 删除列表索引 [1] 的位置数据 变量 .pop(索引序号) + +classmates[1] = "li" # 把索引位置 [1] 的变量替换为 "li" 变量[序号索引] = 变量 + + +classmates = ("wen","liu","wang") # 元组tuple一旦初始化就不能修改 变量 = (变量0,变量1,变量2,...) +# 1、现在,classmates这个tuple不能变了,它也没有append(),insert()这样的方法。 +# 2、其他获取元素的方法和list是一样的,你可以正常地使用classmates[0],classmates[-1],但不能赋值成另外的元素。 +# 3、因为tuple不可变,所以代码更安全 + +# -*- coding: utf-8 -*- + +L = [ + ['Apple', 'Google', 'Microsoft'], + ['Java', 'Python', 'Ruby', 'PHP'], + ['Adam', 'Bart', 'Lisa'] +] + +# 打印Apple: +print(L[0][0]) +# 打印Python: +print(L[1][1]) +# 打印Lisa: +print(L[2][2]) +''' + +#条件判断 + +''' +age = int(input('Please input int:')) # input输入的为字符串类型,小括号括起来用int强制转换为整数型 +if age >= 18: # 条件判断是否大于18岁 + print('your age is', age) + print('adult') # 成立则打印 +else: + print('You are juveniles.\nYou age is', age) # 不成立打印 + +''' + +''' +if <条件判断1>: + <执行1> +elif <条件判断2>: + <执行2> +elif <条件判断3>: + <执行3> +else: + <执行4> + ''' + +# 练习 +# 小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数: + +# 低于18.5:过轻 +# 18.5-25:正常 +# 25-28:过重 +# 28-32:肥胖 +# 高于32:严重肥胖 +# 身高 height 体重 weight BMI指数 BMI_index_number +''' +height = float(input('请输入你的身高。单位是m,保留两位小数:')) +weight = float(input('请输入您的体重。单位是kg,保留两位小数:')) +BMI_index_number = weight / (height**2) +if BMI_index_number <= 18.5: + print('过轻') +elif 18.5 < BMI_index_number <= 25: + print('正常') +elif 25 < BMI_index_number <= 28: + print('过重') +elif 28 < BMI_index_number <= 32: + print ('肥胖') +else: + print('严重肥胖') +print(BMI_index_number) +''' + +# 循环 +''' +names = ['Michael', 'Bob', 'Tracy'] +for name in names: # for...in循环,依次把list或tuple中的每个元素迭代出来 + print(name) # for 变量A in 变量 B +''' + +''' +sum = 0 +for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: + sum = sum + x +print(sum) +''' + +# range +# list(range(100)) # [0,1,2,3,4]列出列表 0到4 五个数 + +# while +# 1加到999之和 + +""" +sum = 0 +n = 0 +while n < 1000: + sum = sum + n + n += 1 +print(sum,n) # 打印结果sum为1+2+'''+n-1之和 , n的值 +""" + +""" +L = ['Bart', 'Lisa', 'Adam'] +for name in L: + print("Hello,",name) +""" + +# break +''' +n = 1 +while n <= 100: + if n > 10: # 当n = 11时,条件满足,执行break语句 + break # break语句会结束当前循环 + print(n) + n = n + 1 +print('END') + +''' + +# continue + +''' +n = 0 +while n < 10: + n = n + 1 + if n % 2 == 0: # 如果n是偶数,执行continue语句 + continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行 + print(n) + +''' +# ******* break 和 continue 搭配 if 语句使用 ******* + +# 廖雪峰py进度 https://www.liaoxuefeng.com/wiki/1016959663602400/1017104324028448 2021.08.24 + +# 使用dict和set + + +""" +d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} # 变量 = {'变量':数值,‘变量':数值,...} +d['Michael'] # 变量['变量'] +95 + +要避免key不存在的错误,有两种办法,一是通过in判断key是否存在: +'Thomas' in d +False + + +二是通过dict提供的get()方法,如果key不存在,可以返回None,或者自己指定的value: +d.get('Thomas') +d.get('Thomas', -1) #变量 'Thomas' 不在字典里则返回数值为-1.其中-1可以自己设定其他数值 +-1 + + +要删除一个key,用pop(key)方法,对应的value也会从dict中删除: +d.pop('Bob') +d +{'Michael': 95, 'Tracy': 85} # 输出结果已删除 Bob + +""" + + +# dict可以用在需要高速查找的很多地方,在Python代码中几乎无处不在,正确使用dict非常重要, +# 需要牢记的第一条就是dict的key必须是不可变对象。 + +# 这是因为dict根据key来计算value的存储位置,如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了。 +# 这个通过key计算位置的算法称为哈希算法(Hash)。 + +# 要保证hash的正确性,作为key的对象就不能变。在Python中,字符串、整数等都是不可变的,因此,可以放心地作为key。 +# 而list是可变的,就不能作为key: + + + +# set + +'''set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。 + +要创建一个set,需要提供一个list作为输入集合:''' + + # s = set([1, 2, 3]) # 变量 = set(['aq',2,1.31, ....]) + # 显示的顺序也不表示set是有序的。重复元素在set中自动被过滤: + + # 通过add(key)方法可以添加元素到set中,可以重复添加,但不会有效果: +''' +>>> s.add(4) +>>> s +{1, 2, 3, 4} +>>> s.add(4) +>>> s +{1, 2, 3, 4}''' + +# 通过remove(key)方法可以删除元素: +''' +>>> s.remove(4) +>>> s +{1, 2, 3} +''' +#****************set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作******** +''' +>>> s1 = set([1, 2, 3]) +>>> s2 = set([2, 3, 4]) +>>> s1 & s2 +{2, 3} +>>> s1 | s2 +{1, 2, 3, 4} +''' + +"""再议不可变对象 +上面我们讲了,str是不变对象,而list是可变对象。 + +对于可变对象,比如list,对list进行操作,list内部的内容是会变化的,比如: + +>>> a = ['c', 'b', 'a'] +>>> a.sort() +>>> a +['a', 'b', 'c'] +而对于不可变对象,比如str,对str进行操作呢: + +>>> a = 'abc' +>>> a.replace('a', 'A') +'Abc' +>>> a +'abc' +虽然字符串有个replace()方法,也确实变出了'Abc',但变量a最后仍是'abc',应该怎么理解呢? + +我们先把代码改成下面这样: + +>>> a = 'abc' +>>> b = a.replace('a', 'A') +>>> b +'Abc' +>>> a +'abc' +要始终牢记的是,a是变量,而'abc'才是字符串对象!有些时候, +我们经常说,对象a的内容是'abc',但其实是指,a本身是一个变量,它指向的对象的内容才是'abc': + +┌───┐ ┌───────┐ +│ a │─────────────────>│ 'abc' │ +└───┘ └───────┘ +当我们调用a.replace('a', 'A')时,实际上调用方法replace是作用在字符串对象'abc'上的, +而这个方法虽然名字叫replace,但却没有改变字符串'abc'的内容。 +相反,replace方法创建了一个新字符串'Abc'并返回,如果我们用变量b指向该新字符串,就容易理解了, +变量a仍指向原有的字符串'abc',但变量b却指向新字符串'Abc'了: + +┌───┐ ┌───────┐ +│ a │─────────────────>│ 'abc' │ +└───┘ └───────┘ +┌───┐ ┌───────┐ +│ b │─────────────────>│ 'Abc' │ +└───┘ └───────┘ +所以,对于不变对象来说,调用对象自身的任意方法,也不会改变该对象自身的内容。 +相反,这些方法会创建新的对象并返回,这样,就保证了不可变对象本身永远是不可变的。 +""" diff --git "a/study/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.py" "b/study/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.py" index f0db4b3..4cc0119 100644 --- "a/study/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.py" +++ "b/study/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.py" @@ -1,3 +1,5 @@ + +# -*- coding:UTF-8 -*- import requests import time import os @@ -6,11 +8,10 @@ resp = requests.get(url) resp = resp.json() + for date in resp["data"]: - sj = date[0] - sj = str(sj) - sj = sj[:-3] - sj = int(sj) + sj = str(date[0]) + sj = int((sj[:-3])) time_local = time.localtime(sj) #转换成新的时间格式(2016-05-05 20:28:54) dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local) @@ -18,4 +19,14 @@ last_price = str(date[4]) high_price = str(date[2]) low_price = str(date[3]) - print(dt,begin_price,last_price,high_price,low_price) + + # 数据写入文件 + with open('比特币价格.csv','a',encoding='utf-8') as f: + f.write('日期:%s,开始价格:%s,收盘价格:%s,最高价格:%s,最低价格:%s,单位美元' %(dt,begin_price,last_price,high_price,low_price)) + f.write('\n') + with open('比特币价格.txt','a',encoding='gbk') as f: + f.write('日期:%s,开始价格:%s,收盘价格:%s,最高价格:%s,最低价格:%s,单位美元' %(dt,begin_price,last_price,high_price,low_price)) + f.write('\n') + +print('获取成功\n') +input('按任意键关闭:') \ No newline at end of file diff --git a/web_spider/download_book.py b/web_spider/download_book.py new file mode 100644 index 0000000..79bb5e8 --- /dev/null +++ b/web_spider/download_book.py @@ -0,0 +1,38 @@ +import requests +import time +from tqdm import tqdm +from bs4 import BeautifulSoup + +wangzhan = input('请输入网址:') +shuming = input('请输入书名:') +muluulr = input('请输入书名ulr:') + + +def get_content(target): + req = requests.get(url = target) + req.encoding = 'utf-8' + html = req.text + bf = BeautifulSoup(html, 'lxml') + texts = bf.find('div', id='content') + content = texts.text.strip().split('\xa0'*4) + return content + +if __name__ == '__main__': + server = wangzhan + book_name = shuming + '.txt' + target = muluulr + req = requests.get(url = target) + req.encoding = 'utf-8' + html = req.text + chapter_bs = BeautifulSoup(html, 'lxml') + chapters = chapter_bs.find('div', id='readerlists') + chapters = chapters.find_all('a') + for chapter in tqdm(chapters): + chapter_name = chapter.string + url = server + chapter.get('href') + content = get_content(url) + with open(book_name, 'a', encoding='utf-8') as f: + f.write(chapter_name) + f.write('\n') + f.write('\n'.join(content)) + f.write('\n') \ No newline at end of file diff --git a/web_spider/notes_2021_8_24.py b/web_spider/notes_2021_8_24.py new file mode 100644 index 0000000..8d702fd --- /dev/null +++ b/web_spider/notes_2021_8_24.py @@ -0,0 +1,119 @@ +#************************** Web Spider of python3 ******************************** +#************************** Date 2021.8.24 ******************************** +#************************** learnning URL ******************************** +# https://mp.weixin.qq.com/s?__biz=MzIxODg1OTk1MA==&mid=2247484915&idx=1&sn=204b7b62d7411cda53623fa69a56aa91&scene=19#wechat_redirect +# 微信公众号: Jack Cui + + + +''' +网络爬虫的第一步就是根据 URL ,获取网页的 HTML 信息。在 Python3 中,可以使用 urllib.request 和 requests 进行网页爬取。 +urllib 库是 Python 内置的,无需我们额外安装,只要安装了 Python 就可以使用这个库。 +requests 库是第三方库,需要我们自己安装。 +requests 库强大好用,后续文章的实例,也都是以此为基础进行讲解。requests 库的 github 地址: + +https://github.com/requests/requests + +1、requests 安装 +在 cmd 中,使用如下指令安装 requests : + +pip install requests + +或者: + +easy_install requests + +2、用法 +官方教程地址: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html +''' + + +"""# -*- coding:UTF-8 -*- +import requests + +if __name__ == '__main__': + target = "http://fanyi.baidu.com/" + req = requests.get(url = target) + req.encoding = 'utf-8' + print(req.text)""" + +# 2、爬虫其实很简单,可以大致分为三个步骤: + +# A、发起请求:我们需要先明确如何发起 HTTP 请求,获取到数据。 +# B、解析数据:获取到的数据乱七八糟的,我们需要提取出我们想要的数据。 +# C、保存数据:将我们想要的数据,保存下载。 + +# 发起请求,我们就用 requests 就行,上篇文章已经介绍过。 + +# 解析数据工具有很多,比如xpath、Beautiful Soup、正则表达式等。本文就用一个简单的经典小工具,Beautiful Soup来解析数据。 + +# 保存数据,就是常规的文本保存。 + +# 3、Beautiful Soup库 的安装 + +''' +简单来说,Beautiful Soup 是 Python 的一个第三方库,主要帮助我们解析网页数据。 +在使用这个工具前,我们需要先安装,在 cmd 中,使用 pip 或 easy_install 安装即可。 +''' +# pip install beautifulsoup4 +# 或者 +# easy_install beautifulsoup4 +'''安装好后,我们还需要安装 lxml,这是解析 HTML 需要用到的依赖''' +# pip install lxml + +''' +Beautiful Soup 的使用方法也很简单,可以看下我在 CSDN 的讲解或者官方教程学习,详细的使用方法: +我的 Beautiful Soup 讲解: + +https://blog.csdn.net/c406495762/article/details/71158264 + +官方中文教程: + +https://beautifulsoup.readthedocs.io/zh_CN/latest/ +''' + + + + + + + +import requests +# import time +# from tqdm import tqdm +from bs4 import BeautifulSoup +""" +def get_content(target): + req = requests.get(url = target) + req.encoding = 'utf-8' + html = req.text + bf = BeautifulSoup(html, 'lxml') + texts = bf.find('div', id='content') + content = texts.text.strip().split('\xa0'*4) + return content +""" +if __name__ == '__main__': + server = 'https://www.imiaobige.com/' + book_name = '西游,开局观音姐姐要和我打扑克.txt' + target = 'https://www.imiaobige.com/read/293121/' + req = requests.get(url = target) + req.encoding = 'utf-8' + html = req.text + chapter_bs = BeautifulSoup(html, 'lxml') + chapters = chapter_bs.find('div', id='readerlists') + chapters = chapters.find_all('a') + for chapter in tqdm(chapters): + print(chapter) + """ + chapter_name = chapter.string + url = server + chapter.get('href') + content = get_content(url) + with open(book_name, 'a', encoding='utf-8') as f: + f.write(chapter_name) + f.write('\n') + f.write('\n'.join(content)) + f.write('\n') + """ + + + diff --git "a/web_spider/python_spider/requests\345\272\223\347\224\250\346\263\225.png" "b/web_spider/python_spider/requests\345\272\223\347\224\250\346\263\225.png" new file mode 100644 index 0000000..5a01dca Binary files /dev/null and "b/web_spider/python_spider/requests\345\272\223\347\224\250\346\263\225.png" differ diff --git a/web_spider/zhengwentiqu-1.py b/web_spider/zhengwentiqu-1.py new file mode 100644 index 0000000..9dc9b50 --- /dev/null +++ b/web_spider/zhengwentiqu-1.py @@ -0,0 +1,12 @@ + +import requests +from bs4 import BeautifulSoup + +if __name__ == '__main__': + target = 'https://www.imiaobige.com/read/293121/1583374.html' + req = requests.get(url = target) + req.encoding = 'utf-8' + html = req.text + bs = BeautifulSoup(html, 'lxml') + texts = bs.find('div', id='content') + print(texts.text.strip().split('\xa0'*4)) \ No newline at end of file diff --git "a/web_spider/\345\246\226\347\245\236\350\256\260.py" "b/web_spider/\345\246\226\347\245\236\350\256\260.py" new file mode 100644 index 0000000..6406254 --- /dev/null +++ "b/web_spider/\345\246\226\347\245\236\350\256\260.py" @@ -0,0 +1,75 @@ + +import requests +import os +import re +from bs4 import BeautifulSoup +from contextlib import closing +from tqdm import tqdm +import time + +""" + Author: + Jack Cui + Wechat: + https://mp.weixin.qq.com/s/OCWwRVDFNslIuKyiCVUoTA +""" + +# 创建保存目录 +save_dir = '妖神记' +if save_dir not in os.listdir('./'): + os.mkdir(save_dir) + +target_url = "https://www.dmzj.com/info/yaoshenji.html" + +# 获取动漫章节链接和章节名 +r = requests.get(url = target_url) +bs = BeautifulSoup(r.text, 'lxml') +list_con_li = bs.find('ul', class_="list_con_li") +cartoon_list = list_con_li.find_all('a') +chapter_names = [] +chapter_urls = [] +for cartoon in cartoon_list: + href = cartoon.get('href') + name = cartoon.text + chapter_names.insert(0, name) + chapter_urls.insert(0, href) + +# 下载漫画 +for i, url in enumerate(tqdm(chapter_urls)): + download_header = { + 'Referer': url + } + name = chapter_names[i] + # 去掉. + while '.' in name: + name = name.replace('.', '') + chapter_save_dir = os.path.join(save_dir, name) + if name not in os.listdir(save_dir): + os.mkdir(chapter_save_dir) + r = requests.get(url = url) + html = BeautifulSoup(r.text, 'lxml') + script_info = html.script + pics = re.findall('\d{13,14}', str(script_info)) + for j, pic in enumerate(pics): + if len(pic) == 13: + pics[j] = pic + '0' + pics = sorted(pics, key=lambda x:int(x)) + chapterpic_hou = re.findall('\|(\d{5})\|', str(script_info))[0] + chapterpic_qian = re.findall('\|(\d{4})\|', str(script_info))[0] + for idx, pic in enumerate(pics): + if pic[-1] == '0': + url = 'https://images.dmzj.com/img/chapterpic/' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic[:-1] + '.jpg' + else: + url = 'https://images.dmzj.com/img/chapterpic/' + chapterpic_qian + '/' + chapterpic_hou + '/' + pic + '.jpg' + pic_name = '%03d.jpg' % (idx + 1) + pic_save_path = os.path.join(chapter_save_dir, pic_name) + with closing(requests.get(url, headers = download_header, stream = True)) as response: + chunk_size = 1024 + content_size = int(response.headers['content-length']) + if response.status_code == 200: + with open(pic_save_path, "wb") as file: + for data in response.iter_content(chunk_size=chunk_size): + file.write(data) + else: + print('链接异常') + time.sleep(10) \ No newline at end of file diff --git "a/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.exe" "b/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.exe" new file mode 100644 index 0000000..47dd530 Binary files /dev/null and "b/\346\257\224\347\211\271\345\270\201\344\273\267\346\240\274\350\216\267\345\217\226.exe" differ