旧的代码如下:


from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def get_driver():
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-gpu")
    options.add_argument("--remote-debugging-port=9222")

    driver = webdriver.Chrome(executable_path='/www/wwwroot/chat.com/addons/chat/py/chromedriver-linux64/chromedriver', options=options)
    return driver


def main(url):
    driver = get_driver()

    try:
        driver.get(url)
        print(driver.page_source)

    except Exception as e:
        traceback.print_exc()

    finally:
        driver.quit()

if __name__ == '__main__':
    url = "http://yyz2.1888.national-lotterys.net/login.html"
    main(url)

在命令行执行没有问题,使用php的shell_exec执行文件报错:

Traceback (most recent call last):
  File "/www/wwwroot/chat.com/addons/chat/py/chrome.py", line 21, in main
    print(driver.page_source)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 61-64: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/www/wwwroot/chat.com/addons/chat/py/chrome.py", line 31, in <module>
    main(url)
  File "/www/wwwroot/chat.com/addons/chat/py/chrome.py", line 24, in main
    traceback.print_exc()
NameError: name 'traceback' is not defined

最后修改python文件如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import traceback
import sys
import io

# Set default encoding to utf-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

def get_driver():
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-gpu")
    options.add_argument("--remote-debugging-port=9222")

    driver = webdriver.Chrome(executable_path='/www/wwwroot/chat.com/addons/chat/py/chromedriver-linux64/chromedriver', options=options)
    return driver


def main(url):
    driver = get_driver()

    try:
        driver.get(url)
        # 确保在终端或日志中正确打印Unicode字符
        print(driver.page_source.encode('utf-8').decode('utf-8'))

    except Exception as e:
        traceback.print_exc()

    finally:
        driver.quit()

if __name__ == '__main__':
    url = "https://1688.666.macao-lottery.com/login.html"
    main(url)

不再报错,问题解决。