ubuntu 启动命令
终端输入如下命令,启动浏览器,端口:9222
google-chrome --remote-debugging-port=9222 --user-data-dir='/home/aiyingfeng/spider/python3/spider/electronic_commerce/crawler_douyin/ch'
端口配置:–remote-debugging-port=9222
用户数据目录:–user-data-dir
python代码
配置文件中加入如下代码,端口:9222,这样就可以控制浏览器
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
完整的python代码
from multiprocessing import Queue
from selenium import webdriver
from spider.base import Base
import time
class xxxxx(Base):
def __init__(self, **kwargs):
super(xxxxx, self).__init__()
self.queue_list = Queue()
self.status_type = "status"
executablePath = r"../file/chromedriver"
self.executablePath = kwargs.get("executablePath", executablePath)
options = webdriver.ChromeOptions()
# 配置代码
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
options.add_argument("--headless")
self.options = {
"headless": True,
"handleSIGINT": True,
"handleSIGTERM": True,
"handleSIGHUP": True,
}
if self.executablePath is not None:
self.options["executablePath"] = self.executablePath
self.browser = webdriver.Chrome(executable_path=self.executablePath, chrome_options=options)
self.browser.get('https://web.jackyun.com/home/mainframe_web.html')
def get_sign_url(self, keywords):
try:
pass
except Exception:
pass
def run(self):
while True:
self.log(f"等待 2 秒")
time.sleep(2)
self.get_sign_url('JY20220616004390')
def close(self):
self.browser.close()
self.browser.quit()
if __name__ == "__main__":
browser = xxxxx()
browser.run()
browser.close()