Python爬虫
个人技术整理,希望达到相互交流的结果
春风化作秋雨
学而思,思而学
展开
-
python模式匹配批量删除redis中的key
1、redis单例模式前提:安装 pip install redisimport redisr = redis.Redis(host='192.168.30.153', port=6383,db=0,decode_responses=True)list_keys = r.keys("http*")for key in list_keys:r.delete(key...原创 2019-05-13 15:30:08 · 5207 阅读 · 0 评论 -
解决:python编程中AttributeError: 'str' object has no attribute 'sleep'
python编程,使用time.sleep(10)挂起线程时候,常见如下异常:time无sleep函数解决方案:方案一:将from time import换成 import time方案二:检查是否有定义名称为time的变量,如有换位其他名称,避免重复。...原创 2019-05-13 10:19:04 · 16362 阅读 · 1 评论 -
Python连接redis实现有序队列queue先进先出/后进先出
目的:实现有序队列queue;先进先出/后进先出。贴代码:from rediscluster import StrictRedisClusterdef redis_queue(): redis_nodes = [ {'host': '192.168.30.153', 'port': 6383}, {'host': '192.168.30.15...原创 2019-04-30 15:43:06 · 2537 阅读 · 0 评论 -
Python3连接myqsql数据库_增删改查
前提:安装三方库# pip3 install PyMySQL1、连接数据库db = pymysql.connect("192.168.30.150", "workhr", "YDJ9E1:o}M", "workemployee", charset='utf8')参数说明:IP地址,用户名,密码,数据库名,编码#!/usr/bin/python# -*- codi...原创 2019-04-30 14:13:04 · 428 阅读 · 0 评论 -
Python连接Redis
前提:安装redis插件# pip install redis-py-cluster1、redis单例模式import redisr = redis.Redis(host='192.168.30.153', port=6384)r.set('py_key', 'hello world')r.get('py_key')说明:如果你连接的redis是集群模式,会报异常...原创 2019-04-29 10:29:50 · 715 阅读 · 0 评论 -
Python日志输出管理既可在控制打印,又能记入日志文件,且能按级别输出
要求:1、在控制台打印日志;2、记如指定日志文件;3、按不同的级别输出;4、定义日志工具类,减少代码量。贴代码import loggingimport sysimport osroot_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(sys.argv[0]))))LOG_...原创 2019-04-26 18:24:06 · 1073 阅读 · 0 评论 -
Linux下后台运行python程序,并输出日志文件
场景:Python程序执行需要很长的时间或者需要一直在跑,或者需要打印并记录很多信息策略:后台运行程序,然后把打印信息保存在指定的文件中,等程序运行结束后,再检查输出日志或者实时观察日志。1、启动命令:1、一般使用nohup python -u Job.py > log.log 2>&1 &2、只记录异常日志(推荐)# nohup pyth...原创 2019-04-26 11:40:59 · 24176 阅读 · 2 评论 -
解决:Linux下-bash: nohup: command not found或者bash: nohup: 未找到命令
首先,没有发现nohup,先安装# yum install coreutils其次,如果已经安装 ,查看本地是否有,查看nohup具体位置# which nohup再次,将具体位置进行配置#vi ~/.bash_profile然后,保存,刷新刷新生效# :wq# source ~/.bash_profile最后,进行验证# ...原创 2019-04-26 10:16:28 · 17720 阅读 · 3 评论 -
解决:File "/usr/bin/yum", line 30 或 File "/usr/libexec/urlgrabber-ext-down", line 28
在执行yum命令时忽然发现出现以下报错:# yum install coreutilsFile"/usr/bin/yum", line 30except KeyboardInterrupt, e:^SyntaxError: invalid syntax问题原因:这是因为yum采用Python作为命令解释器,这可以从/...原创 2019-04-26 09:55:57 · 916 阅读 · 0 评论 -
Python发送邮件
Python发送邮件需要两个包:smtplib 用来发送邮件email 用来构建邮件。Python 的 email 模块里包含了许多实用的邮件格式设置函数,用来创建邮件。使用的 MIMEText 对象,为底层的MIME协议传输创建了一封空邮件,最后通过SMTP 协议发送出去。 MIMEText 对象 msg 包括收发邮箱地址、邮件正文和主题,Python 通过MIMEText 就...原创 2019-04-25 14:53:17 · 795 阅读 · 0 评论 -
解决:Python ModuleNotFoundError: No module named 'com'
问题:在pycharm中可以正常执行,但是在cmd的命令窗口,以及linux环境下,一直报出如下异常ModuleNotFoundError: No module named 'com'解决:1、windows环境下2、linux环境下# vim ~/.bash_profile追加PYTHONPATH =代码的根目录exportPYTHONPATH...原创 2019-04-25 10:55:47 · 8554 阅读 · 1 评论 -
关于在Linux下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法
异常:3.7版本需要一个新的包libffi-devel,安装此包之后再次进行编译安装即可。# yum install libffi-devel -y# make install若在安装前移除了/usr/bin下python的文件链接依赖,此时yum无法正常使用,需要自己下载相关软件包安装,为节省读者时间,放上链接# wget http://mirror.centos...原创 2019-04-24 17:29:04 · 5108 阅读 · 0 评论 -
Python中Beautifulsoup去除/过滤掉特定标签
使用如下代码,进行过滤或排除[s.extract() for s in soup('div')]贴代码# coding:utf-8from bs4 import BeautifulSoupsoup = BeautifulSoup('<div>早上9点了</div>你好世界<div>世界和平</div>')info = ...原创 2019-04-24 14:40:19 · 19485 阅读 · 0 评论 -
Python开发爬虫项目+代码
近期,用Python做了一个爬虫项目,爬取各大网站的资讯文章以及视频信息等实现:1、分析网站,分析网站布局,解析分页信息获取方式;2、获取源码,获取整个网页的源码信息;3、分页机制,爬取分页信息,分页信息或者分页请求(返回json数据);4、重试机制,追加尝试机制,爬取网页信息失败,有限次数的重试,避免网络丢包数据丢失;5、分类爬取,爬取文章信息,包括标题,作者,发布时间,...原创 2019-04-24 11:26:14 · 1011 阅读 · 0 评论 -
Pycharm如何不自动打开最近项目,显示项目列表
目标:Pycharm如何不自动打开最近项目,显示项目列表。 路径:File->settings->system settings->startup->reopen lase project on startup。 操作:勾掉"reopen lase project on startup",然后,点击【Apply】或【OK】即可!...原创 2019-04-24 10:55:08 · 4878 阅读 · 1 评论 -
Python字符串与时间相互转换
1、字符串转日期/时间 注意,字符串格式要与参数中时间格式要严格匹配,否则异常 举例: ① 2019-05-01 12:00:00 对应 %Y-%m-%d %H:%M:%S ② 2019-05-01 对应 %Y-%m-%d2、日期/时间转字符串3、打印系统当前时间代码如下:# coding:utf-8import datetimeimpor...原创 2019-04-24 10:34:07 · 20493 阅读 · 0 评论 -
Ptython时间大小比较
Python中,time模块,主要有一下方法:1、time.time() 获取当前系统时间,返回float型数值时间戳(当前时间相对于1970.1.1 00:00:00 以秒计算的偏移量)2、time.localtime() 获取当前系统时间,返回struct time 型时间3、time.mktime() 把struct time转换成float型时间戳问题:如何将字符串时间和系统当前...原创 2019-04-24 10:22:19 · 1027 阅读 · 1 评论 -
关于python无法显示中文的问题:File "testpy", line 5 SyntaxError: Non-ASCII character '\xe6' in file test
问题:编码问题解决方案:解决办法: 在以后的每一个需要显示汉字的python文件中, 可以采用如下方法在#!/usr/bin/python的下一行加上一句话来定义编码格式, 我以utf-8编码为例。 第一种:#!/usr/bin/python#coding:utf-8print("国际泰山") 第二种: #!/usr/bin/python #-*-co...原创 2019-04-23 17:12:22 · 1297 阅读 · 0 评论 -
linux下安装python3.7.3
如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境,比如yum!!!!!不要动现有的python2环境!一、安装python3.7.31. 安装依赖环境 #yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sql...原创 2019-04-22 16:49:39 · 12585 阅读 · 3 评论 -
Python多线程编程实现
贴代码:import threadingimport timelist = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]class myThread(threading.Thread): def __init__(self, threadId, name, counter): threading.Thread.__init__(...原创 2019-04-17 20:25:14 · 226 阅读 · 0 评论 -
Python定时任务
1、间隔时间执行import threadingdef execute(): print("开始执行...") timer = threading.Timer(5, execute) timer.start()if __name__ == "__main__": execute()2、指定时间执行import timewhil...原创 2019-04-17 20:05:30 · 471 阅读 · 3 评论 -
Python异常处理
异常捕获,避免程序异常,无法继续往下执行for i in range(0, 10): try: 1 / 0 except Exception as e: print('except:', e)原创 2019-04-17 20:03:00 · 148 阅读 · 0 评论 -
Python之base64加密解密
import base64msg = 'good night 李明'msg = msg.encode('utf-8')# 加密bs64 = base64.b64encode(msg)print(bs64)# 结果是 b'Z29vZCBuaWdodA=='# 解密debs64 = base64.b64decode(bs64)debs64 = debs64.decode('...原创 2019-04-17 17:50:54 · 4814 阅读 · 1 评论 -
Python http +Post+ json请求
贴代码from urllib import parse, requestimport jsonparameter = {"id": 0}# json串数据使用parameter = json.dumps(parameter).encode(encoding='utf-8')# 普通数据使用# parameter = parse.urlencode(parameter).en...原创 2019-04-16 17:09:39 · 8236 阅读 · 0 评论 -
Python去掉字符串所有空格
贴代码import restr = 'I HATE WHAT I I LOVE 'str = re.sub('\s+', '', str).strip()print(str)原创 2019-04-16 15:45:54 · 10179 阅读 · 0 评论 -
Python移动复制文件
直接贴代码import os, shutildef movefile(srcfile, dstfile): if not os.path.isfile(srcfile): print("%s not exist!" % (srcfile)) else: fpath, fname = os.path.split(dstfile) # ...原创 2019-04-15 17:18:00 · 646 阅读 · 0 评论 -
Python对文件进行重命名
直接贴代码:import ossrcFile = './actwork/linkFile/allExtLinks - 副本.txt'dstFile = './actwork/linkFile/allExtLinks - copy.txt'try: os.rename(srcFile,dstFile)except Exception as e: print(e)...原创 2019-04-15 17:12:05 · 59500 阅读 · 4 评论 -
Python获取并输出当前日期当前时间
import timeprint(time.time())print(time.strftime('%Y%m%d',time.localtime(time.time())))print(time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())))详解:取得时间相关的信息的话,要用到python time模块,pyt...原创 2019-04-15 17:04:51 · 19753 阅读 · 0 评论 -
ModuleNotFoundError: No module named 'aiohttp'
pip install aiohttp原创 2019-04-11 14:27:20 · 7682 阅读 · 0 评论 -
python爬取CSDN文章保存至本地
# encoding:utf-8__author__ = 'Sun'from workTest import CsdnBlogSpiderfrom tkinter import *import tkinter as tkimport tkinter.messagebox as messageboximport tkinter.fontimport threadingimport...原创 2019-04-11 11:57:12 · 1152 阅读 · 2 评论 -
python特殊字符替换
str = 'adsdf*sd\dsaa/avc*?><|?,'print(str)print()result = eval(repr(str).replace('\\', ''))result = eval(repr(result).replace('/', ''))result = eval(repr(result).replace('*', ''))result ...原创 2019-04-11 11:54:45 · 11235 阅读 · 1 评论