python 问题
requets 记录
设置了代理,但是代理却不起作用
这个问题,可能是因为设置的代理协议和要访问的网站协议不一致造成的。比如博客协议是https,如果代理协议是http,则代理将不会起作用。修改一下 demo,试验之:
#encoding=utf8
import requests
url = 'https://blog.popkx.com'
proxy = {
'https': 'https://95.154.110.194:51232'
}
header = {
'User-agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
}
if __name__ == "__main__":
res = requests.get(url, headers=header, proxies=proxy)
print res.status_code
Python获取URL中参数的方法
pip install urllib
from urllib import parse
>>> url = 'https://www.google.com/search?newwindow=1&biw=1091&bih=763'
>>> from urllib import parse
>>> params = parse.parse_qs( parse.urlparse( url ).query )
>>> params['newwindow']
['1']
>>> params['biw']
Library not loaded: /usr/local/opt/libffi/lib/libffi.6.dylib
brew reinstall libffi
==> Downloading https://homebrew.bintray.com/bottles/libffi-3.3.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Reinstalling libffi
==> Pouring libffi-3.3.catalina.bottle.tar.gz
==> Caveats
libffi is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
For compilers to find libffi you may need to set:
export LDFLAGS="-L/usr/local/opt/libffi/lib"
export CPPFLAGS="-I/usr/local/opt/libffi/include"
For pkg-config to find libffi you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
==> Summary
🍺 /usr/local/Cellar/libffi/3.3: 16 files, 489.4KB
Removing: /usr/local/Cellar/libffi/3.2.1... (16 files, 297.0KB)
pip 安装whl
pip install XXX.whl
pip 更新包
#列出所有已安装的三方库
pip list
#列出当前已安装的第三方库中所有过期的库
pip list --outdated
pip install --upgrade 库名
#列出所有已过期的库
pip list --outdated
#单个更新库名
pip install --upgrade 库名
import pip
from subprocess import call
from pip._internal.utils.misc import get_installed_distributions
# for dist in pip.get_installed_distributions():
# call("pip install --upgrade " + dist.project_name, shell=True)
for dist in get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
python 函数
*args 和 **kwargs
*args 和 **kwargs 主要用于函数定义。 你可以将不定数量的参数传递给一个函数。
def test_var_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv:", arg)
test_var_args('yasoob', 'python', 'eggs', 'test')
网友评论