设置cookie:
headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Mobile Safari/537.36',
'cookie':'PHPSESSID=ahigi5gtv3ujkd89usu0dvl397'
}
使用代理:
import requests proxies = {'http': 'http://localhost:8888', 'https': 'http://localhost:8888'} url = 'http://www.baidu.com' requests.post(url, proxies=proxies, verify=False)
使用socket代理:
安装socks:pip3 install PySocks
headers = {'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Mobile Safari/537.36',
'cookie':'PHPSESSID=ahigi5gtv3ujkd89usu0dvl397'
}
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 51837)
socket.socket = socks.socksocket
f = requests.get(url, timeout=2, headers=headers, verify=False)
request忽略https:
from requests.packages.urllib3.exceptions import InsecureRequestWarning # 禁用安全请求警告 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
response=requests.get(url,verify=False)
逐行读取:
for line in open("/pythontab/code.txt"): print(line)
使用request报错InsecureRequestWarning: Unverified HTTPS request is being made.解决方法
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
测试时间:
import time
#开始时间
t1 = time.time()
#结束时间
t2 = time.time()
print(f"newdata length:\t{len(newdata)}")
print(f"time use:\t{round(t2 - t1,3)}s")
解决端口占用socket.error: [Errno 48] Address already in use
打开终端,输入sudo lsof -i:端口号
然后执行kill PID
解决循环打开多个word文档时报错:
若我先手动打开一个Word文件,这时就运行了一个word进程。若执行Dispatch创建进程。
- word = Dispatch('word.application')
再设置word不可见。
- word.visible = False
但,我手动打开的Word进程的界面被隐藏了。
这说明一个问题,Dispatch若发现该进程已经存在的话,就不会创建新的进程。
若不创建新的进程,有些操作会有冲突,可能会影响到已经打开的Word文件。
这时,不能再使用Dispatch,用如下代码:
- from win32com.client import DispatchEx
- word = DispatchEx('word.application')
即可真正创建新进程,无论是否已经打开了一样的进程。
升级pip安装的包:
检查哪些包需要升级
pip list --outdated
升级包:
pip install --upgrade 要升级的包名
卸载包:
pip uninstall 要卸载的包名
修改pip源:
- 找到系统盘下C:\C:\Users\用户名\AppData\Roaming,AppData是隐藏目录
- 查看在Roaming文件夹下有没有一个pip文件夹,如果没有创建一个;
- 进入pip文件夹,创建一个pip.ini文件;
- 使用记事本的方式打开pip.ini文件,写入:
[global] index-url = http://mirrors.aliyun.com/pypi/simple # 指定下载源 trusted-host = mirrors.aliyun.com # 指定域名
python生成pyc文件:
对于py文件,可以执行下面命令来生成pyc文件。
python -m foo.py
另外一种方式是通过代码来生成pyc文件。
import py_compile py_compile.compile('/path/to/foo.py')
批量生成pyc代码:
import compileall compileall.compile_dir(r'/path')
命令行格式:
python -m compileall <dir>
解决from docx import Document报错:
pip install python-docx
解决python3和python3共存中,分别使用pip和pip3:
python3-m pip install --upgrade pip --force-reinstall
python2-m pip install --upgrade pip --force-reinstall
快速搭建http服务:
python -m SimpleHTTPServer
解决使用pip安装第三方包报错:

使用国内镜像 pip3 install poster -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
或者使用:pip install --trusted-host pypi.python.org requests
安装python虚拟环境及使用
$ sudo pip install virtualenv
然后建立一个测试目录:
$ mkdir testvirtual $ cd testvirtual
就可以成功创建一个虚拟环境 env1:
$ virtualenv env1
$ cd env1/bin
$ source activate
退出虚拟环境:deactivate
安装pyspider出现的问题:

需要升级openssl
$ brew install openssl
$ brew upgrade openssl
$ brew link openssl --force
之后重新安装pycurl
pip uninstall pycurl# 卸载库 export PYCURL_SSL_LIBRARY=openssl export LDFLAGS=-L/usr/local/opt/openssl/lib export CPPFLAGS=-I/usr/local/opt/openssl/include# openssl相关头文件路径 pip install pycurl --compile --no-cache-dir# 重新编译安装
|