随笔分类 - python
python学习
摘要:pip install python.docx from docx import DocumentDoc = Document() 解释:from 从 docx这个文件中,导入一个叫Document的一个东西,Document是文档的意思,所以它是对word文档进行操作的一个玩意. 在下面Doc =
阅读全文
摘要:import requests rr=requests.get("https://api.github.com",auth=('user','pass')) print(rr.status_code) print(rr.headers['content-type']) 结果: RESTART: D:
阅读全文
摘要:urls=[f'https://www.baidu.com/?page={page}' for page in range(1,5)] #F f大小写都可以 print(urls) page=10 url='https://www.baidu.com/?page={}'.format(page) p
阅读全文
摘要:>>> b=(page for page in range(10))>>> print(b)<generator object <genexpr> at 0x0000000002EE61C8>>>> list(b) #只能生成一次[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> l
阅读全文
摘要://img/@src 得到所有img标签的src值 //a/text() 得到所有A标签中的文本 name=response.xpath('//img@src').getall() #getall()从对象中获取具体值 import requests,re from lxml import etre
阅读全文
摘要:r'[\u4e00-\u9fa5]汉字 .匹配除换行符外的任意字符 \d匹配所有数字 ,与[0-9]相同[0123456789] \D匹配非数字字符,[^0-9] \w匹配数字,字母,下划线[0-9a-zA-Z_] \W匹配非数字 字线 下划线[^0-9A-Za-z] \s匹配任意空白符(空格 换行
阅读全文
摘要:执行过程中出错,复制错误信息,利用TRY得到相应的错误,并用特别方法解决: print(3/0)提示: Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> print(3/0)ZeroDivisionE
阅读全文
摘要:import itertools mylist=list(itertools.permutations([1,2,3,4],3)) #排列 print(mylist) print(len(mylist)) mylist=list(itertools.combinations([1,2,3,4],3)
阅读全文
摘要:import os def file_name(file_dir): for root, dirs, files in os.walk(file_dir): print(root) #当前目录路径 print(dirs) #当前路径下所有子目录 print(files) #当前路径下所有非目录子文件
阅读全文
摘要:https://images.weserv.nl/?url=
阅读全文
摘要:Python encode()encode() 方法以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。写法:str.encode(encoding='UTF-8',errors='strict')参数encoding -- 要使用的编码,如"UTF-8"。e
阅读全文
摘要:Python startswith()方法Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查 语法:str.startswith(str, beg=0,end=le
阅读全文
摘要:自定义file_path()函数,即可以原有图像文件名为名来保存,并分类保存 def file_path(self, request, response=None, info=None): image_guid = request.url.split('/')[-2]+"/"+request.url
阅读全文
摘要:近来安装python scrapy经常忘了添加到path,需要时增加了但需要重启才能起作用,用下面的方法马上能生效: 1修改path:右击“计算机”--高级--环境变量--path 2。打开“任务管理器”,结束进程"explorer.exe",再打开.方法:在任务管理器进程中找到“explorer.
阅读全文
摘要:运行scrapy时出错这个错误:Max retries exceeded with url解决方法: img1=requests.get(url=aa,headers=header1,timeout=5,verify=False)爬虫能运行了,但还是报错,但不影响使用
阅读全文
摘要:'''使用walk方法递归遍历目录文件,walk方法会返回一个三元组,分别是root、dirs和files。 其中root是当前正在遍历的目录路径;dirs是一个列表,包含当前正在遍历的目录下所有的子目录名称,不包含该目录下的文件; files也是一个列表,包含当前正在遍历的目录下所有的文件,但不包
阅读全文
摘要:1.建立爬虫项目(结果形成与名称相同的文件夹) scrapy startproject <爬虫项目名称> (下面的所有操作都进入下一级文件进行操作) 2建立一个爬虫 scrapy genspider [- t 模板名称] <爬虫名称> < 爬虫爬取的域名> 3.运行一个爬虫scrapy craw <
阅读全文
摘要:在PY文件中: from scrapy.selector import Selectorfrom scrapy.http import HtmlResponse url="https://m.mm131.net/" r=requests.get(url) r.encoding='gbk' #根据情况
阅读全文
摘要:1。在middlewares中添加自己的新类: class Mylei(object): def process_request(self,request,spider): referer=request.url if referer: request.headers["referer"] = re
阅读全文
摘要:1。安装SCRAPY2。进入CMD:执行:SCRAPY显示: Scrapy 1.8.0 - no active project Usage: scrapy <command> [options] [args] Available commands: bench Run quick benchmark
阅读全文

浙公网安备 33010602011771号