centos安装chrome+chromedriver
一、下载对应的版本:
driver:
https://chromedriver.storage.googleapis.com/index.html?path=103.0.5060.24/
http://dist.control.lth.se/public/CentOS-7/x86_64/google.x86_64/
二、安装
unzip chromedriver.zip
yum install google_chrome...
三、使用python做中转代理
Main.py
- import os
- from fastapi importFastAPI,Request
- import uvicorn
- from fastapi importFastAPI
- fromGoogleUtilimportGoogleUtil
- from fastapi.responses importHTMLResponse
- app =FastAPI()
- @app.get("/")
- def home():
- return'api is ok'
- @app.get("/api", response_class=HTMLResponse)
- def request(request :Request):
- html =''
- http =None
- try:
- http =GoogleUtil()
- url = request.query_params.get('url',None)
- html = http.getString(url, params={})
- http.close()
- exceptExceptionas e:
- print(e)
- http.close()
- return html
- if __name__ =='__main__':
- print('chromedriver:',os.getcwd()+'/chromedriver')
- print('api is running...')
- uvicorn.run(app, host="0.0.0.0", port=8080, log_level='debug')
GoogleUtil.py
- import time
- from selenium import webdriver
- import random
- from lxml import etree
- from Config import Config
- import os
- import json
- class GoogleUtil:
- # driver.get("http://sl.baidu.com/#/home")
- def __init__(self):
- self.chrome_options = webdriver.ChromeOptions()
- self.chrome_options.add_argument("no-sandbox")
- self.chrome_options.add_argument("--user-agent=" + random.choice(Config.user_agents))
- self.chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
- self.chrome_options.add_argument('--headless') # 无界面化浏览器
- self.chrome_options.add_argument('window-size=1080x1920') # 指定浏览器分辨率
- # chrome_options.add_argument('--no-sandbox')
- self.chrome_options.add_argument('--disable-gpu')
- # chrome_options.add_argument('--disable-dev-shm-usage')
- # 文件下载地址
- self.download_dir = os.getcwd() + '/downloads/'
- prefs = {
- 'profile.default_content_settings.popups': 0,
- 'download.default_directory': self.download_dir
- }
- if not os.path.exists(self.download_dir):
- os.mkdir(self.download_dir)
- self.chrome_options.add_experimental_option("prefs", prefs)
- self.chromedriver = os.getcwd() + '/chromedriver'
- os.environ["webdriver.chrome.driver"] = self.chromedriver
- self.driver = webdriver.Chrome(chrome_options=self.chrome_options, executable_path=self.chromedriver)
- pass
- def close(self):
- self.driver.close()
- def get(self, url, params=None, headers=Config.headers):
- print('google driver...')
- if params:
- if url.find('?'):
- url = url + '?'
- for p in params:
- url += '>' + p + '=' + str(params[p])
- try:
- print(url)
- self.driver.get(url)
- self.driver.implicitly_wait(1)
- time.sleep(2)
- html = self.driver.page_source
- if html.find("Our systems have detected") > 0:
- print("Your IP have been blocked by " + url)
- print("change user-agent, retry")
- self.chrome_options.add_argument("--user-agent=" + random.choice(Config.user_agents))
- self.driver = webdriver.Chrome(chrome_options=self.chrome_options, executable_path=self.chromedriver)
- self.driver.get(url)
- time.sleep(2)
- html = self.driver.page_source
- res = etree.HTML(html)
- return res
- except Exception as e:
- print(e)
- return etree.HTML("<html></html>")
- def getString(self, url, params):
- Config.headers["User-Agent"] = random.choice(Config.user_agents)
- try:
- if params:
- if url.find('?'):
- url = url + '?'
- for p in params:
- url += '>' + p + '=' + str(params[p])
- self.driver.get(url)
- res = self.driver.page_source
- return res
- except Exception as e:
- print(e)
- return "<html></html>"
- def getJson(self, url, params):
- Config.headers["User-Agent"] = random.choice(Config.user_agents)
- try:
- if params:
- if url.find('?'):
- url = url + '?'
- for p in params:
- url += '>' + p + '=' + str(params[p])
- self.driver.get(url)
- res = json.loads(self.driver.page_source)
- return res
- except Exception as e:
- print(e)
- return {}
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · centos安装node18
- · CentOS 安装Docker-CE
- · centos yum安装Python3.8(史上最简单)
- · Chrome 便携版【Chrome++】制作
- · CentOS下使用tcpdump网络抓包用
- · centos 7安装kubectl
- · CentOS在线安装RabbitMQ3.7
- · 屏蔽 Chrome 的提示: “请停用以开发者模式运行的扩展程序”
- · Hadoop学习教程(二) —- CentOS下安装JDK
- · CentOS下VMware用桥接模式,静态ip上外网
- · CentOS 6.x 一键安装PPTP VPN脚本
- · centos lamp一键安装包 下载