来源:https://www.cnblogs.com/xiaoaiyiwan/p/10776493.html 稍作修改
1.第一步,代码如下:
from requests_html import HTMLSession
url="https://www.baidu.com/"
headers={ "Host": "www.baidu.com", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
session=HTMLSession() req=session.get(url,headers=headers) req.encoding="utf-8"
req.html.render() result=req.html.find("a.mnav",first=True) print(req.status_code) print(result.text) print(result.attrs.get('href'))
2.因为是第一次使用render函数,需要安装chromium,无奈速度太慢,等待几分钟,才2% 因为各种不可知的原因,下载时还有可能报错 我的报错是
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='storage.googleapis.com', port=443): Max retries exceeded with url: /chromium-browser-snapshots/Win_x64/575458/chrome-win32.zip (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
3.解决步骤如下:
3.1手动下载chromium
https://npm.taobao.org/mirrors/chromium-browser-snapshots/Win_x64/650583/
下载后之后解压。
3.2 requests_html运行chromium的路径究竟是怎么样的?
3.2.1 进入python安装目录下的\Lib\site-packages\pyppeteer目录
笔者的目录是:C:\Users\Ray\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyppeteer
3.2.2 打开chromium_downloader.py文件
找到代码:
chromiumExecutable = { 'linux': DOWNLOADS_FOLDER / REVISION / 'chrome-linux' / 'chrome', 'mac': (DOWNLOADS_FOLDER / REVISION / 'chrome-mac' / 'Chromium.app' / 'Contents' / 'MacOS' / 'Chromium'), 'win32': DOWNLOADS_FOLDER / REVISION / 'chrome-win32' / 'chrome.exe', 'win64': DOWNLOADS_FOLDER / REVISION / 'chrome-win32' / 'chrome.exe', }
从上面可以看出,win64(笔者的win10 系统是64位的)的chromium路径是:
DOWNLOADS_FOLDER / REVISION / 'chrome-win32' / 'chrome.exe',
那么,DOWNLOADS_FOLDER 和REVISION究竟是什么?
往上面寻找,可以找到以下代码:
DOWNLOADS_FOLDER = Path(pyppeteer_home) / 'local-chromium'
REVISION = os.environ.get('PYPPETEER_CHROMIUM_REVISION', chromium_revision)
可以使用print函数打印出两个路径,具体代码如下:
from pyppeteer import chromium_revision, pyppeteer_home
DOWNLOADS_FOLDER = Path(pyppeteer_home) / 'local-chromium'
REVISION = os.environ.get('PYPPETEER_CHROMIUM_REVISION', chromium_revision)
print(DOWNLOADS_FOLDER)
print(REVISION)
直接运行这个py文件,也可以拷贝部分代码去自己的py文件中运行就可以知道两个变量的路径。
我的代码如下
import os from pathlib import Path from pyppeteer import chromium_revision, pyppeteer_home DOWNLOADS_FOLDER = Path(pyppeteer_home) / 'local-chromium' REVISION = os.environ.get('PYPPETEER_CHROMIUM_REVISION', chromium_revision) chromiumExecutable = { 'linux': DOWNLOADS_FOLDER / REVISION / 'chrome-linux' / 'chrome', 'mac': (DOWNLOADS_FOLDER / REVISION / 'chrome-mac' / 'Chromium.app' / 'Contents' / 'MacOS' / 'Chromium'), 'win32': DOWNLOADS_FOLDER / REVISION / 'chrome-win32' / 'chrome.exe', 'win64': DOWNLOADS_FOLDER / REVISION / 'chrome-win32' / 'chrome.exe', } print(chromiumExecutable['win64'])
这样可以直接找到安装的路径。
由上面可以知道:chromium路径是:C:\Users\Ray\AppData\Local\pyppeteer\pyppeteer\local-chromium\575458\chrome-win32\chrome.exe
所以自己建文件夹,然后一直到chrome-win32文件夹,把上面下载的chromium文件,拷贝到此目录下,下载到的Chromeium是一个压缩包,解压后把全部文件拷贝到路径就可以了。
4.运行第一步的代码,完美打印。
具体灵感来源:https://github.com/GoogleChrome/puppeteer/issues/1597 |