打开百度——>输入python——>点击搜索——>获取页面中所有的href并打印
- 下载对应版本的Chromedriver
- 安装Selenium
pip3 install selenium - 编写proxy.py文件,该文件中方法处理代理问题,并将生成的manifest.json和background.js压缩为vimm_chrome_proxyauth_plugin.zip
import string
import zipfile
def create_proxyauth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
# """Proxy Auth Extension
# args:
# proxy_host (str): domain or ip address, ie proxy.domain.com
# proxy_port (int): port
# proxy_username (str): auth username
# proxy_password (str): auth password
# kwargs:
# scheme (str): proxy scheme, default http
# plugin_path (str): absolute path of the extension
# return str -> plugin_path
# """
if plugin_path is None:
plugin_path = 'vimm_chrome_proxyauth_plugin.zip'
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: [""]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=int(proxy_port),
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
- 主要文件
from selenium import webdriver
# 导入选项包
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
from proxy import create_proxyauth_extension
#chromedriver.exe存储位置
s = Service(r"/Users/mac/Desktop/chromedriver")
# 创建chrome参数对象,设置chrome浏览器无界面模式
chrome_options = Options()
# 设置代理
# chrome_options.add_argument("--no-proxy-server")
proxyauth_plugin_path = create_proxyauth_extension("proxy.intra", "80", "name", "psw")
chrome_options.add_extension(proxyauth_plugin_path)
driver = webdriver.Chrome(service=s,options=chrome_options)
driver.get('https://www.baidu.com')
# 输入python
driver.find_element(By.ID,"kw").send_keys("python")
# 点击搜索
driver.find_element(By.ID,"su").click()
# urls = driver.find_elements(By.XPATH,"//a")
try:
# 获取所有的a标签
for link in driver.find_elements(By.XPATH,'//a'):
print(link.get_attribute('href'))
a = link.get_attribute('href')
print(a)
except:
#截图并存储
driver.get_screenshot_as_file("/Users/mac/Desktop/screenshot.png")
finally:
print("==========over=========")
# 关闭
driver.close()