目录
Selenium 对网页控件进行捕获,ps:无头模式下,抓取可能失效
Anaconda 创建python环境
1、创建环境
conda create --name python3-env python=3.7
2、切换环境:
source activate <env_name>
3、退出环境:
source deactivate
进入后,使用python3命令可以使用3.7版本python。否则为系统自带的3.5
4、显示所有环境:
conda info –envs
5、复制环境:
conda create --name <new_env_name> --clone <copied_env_name>
6、删除环境:
conda remove --name <env_name> --all
无法访问github:
上这个网站搜索下面两个网站的ip: https://www.ipaddress.com/
github.com 140.82.112.3
github.global.ssl.fastly.net 199.232.69.194
1.打开文件:/etc/hosts
2.把两个ip地址复制进去
3.打开终端,运行:systemctl restart nscd
Ubuntu安装firefox浏览器 selenium插件
geckodriver(Firefox浏览器)
https://github.com/mozilla/geckodriver/releases
$ unzip chromedriver_linux64.zip
$ sudo mv chromedriver /usr/bin/
$ sudo chmod +x chromedriver
Selenium访问网页:
from selenium import webdriver
driver = webdriver.Chrome()
# driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
print(driver.title)
driver.quit()
Selenium 对网页控件进行捕获,ps:无头模式下,抓取可能失效
brower.find_element_by_id('kw').send_keys('selenium')
brower.find_element_by_id('su').click()
使用Firefox,Chrome headless模式
要在selenium里使用headless版本的Firefox,Chrome的也不难,只需要像正常使用Firefox,Chrome那样即可,只是需要稍稍添加几行代码即可。
方法一:会有警告
chrome_options = webdriver.ChromeOptions()
chrome_options.set_headless()
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.baidu.com')
driver.close()
方法二:无警告,但是在无头情况下,执行获取空间的代码,会报错:
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.headless = True
#opts.add_argument('--headless')
driver = webdriver.Firefox(options=opts)
driver.get('https://www.baidu.com')
print(driver.title)
driver.close()
安装wireshark,并给予权限,否则无法扫描到端口
sudo apt install wireshark 安装wireshark,也可以去ubuntu软件中心安装
sudo dpkg-reconfigure wireshark-common 重置非root用户是否可以捕获流量
sudo chmod +x /usr/bin/dumpcap 修改dumpcap的权限
如何为Anaconda环境添加自己创建的python模块:
Windows系统:
如果未使用虚拟环境:
在E:\Anaconda\Lib\site-packages目录下,创建mypath.pth文件,在文件中写入自己创建的python模块路径即可,如图:
Linux操作系统(ubuntu):
使用虚拟环境:
进入虚拟环境中python所在目录:在/home/jay/anaconda/envs/p3_env/python3.7/site-packages目录下,创建mypath.pth文件。写入自己创建的python模块路径即可。如图:
如何解决idle找不到模块的问题:
Idle运行时,找包的路径根据时使用哪个pythonw.exe来运行idle.py文件
模块找不到时:
python路径:"E:\Anaconda3\ pkgs\python-3.6.3-h9e2ca53_1\pythonw.exe"
Idle路径:"E:\Anaconda3\pkgs\python-3.6.3-h9e2ca53_1\Lib\idlelib\\idle.py
修改为:
Python路径:"E:\Anaconda3\pythonw.exe"
Idle路径:"E:\Anaconda3\pkgs\python-3.6.3-h9e2ca53_1\Lib\idlelib\\idle.py
修改方法:
1.win+R,输入regedit,打开注册表编辑器
2.路径如下:计算机\HKEY_CLASSES_ROOT\SystemFileAssociations\.py\shell\Edit with IDLE\command
3.修改即可