import scrapy
import json
import re
import execjs
import demjson
關(guān)鍵代碼:
def parse_config(self, response):
? ? s = re.findall(r'<script>\(function\((.*?)</script>', response.text, re.M | re.S)
????ss =""
????for ?j in range(0, len(s)):
????????ss +="(function(" + s[j]
????jsstr ='var jsdom = require("jsdom");var { JSDOM } = jsdom; var dom = new JSDOM(); ????????window = dom.window; document = window.document;'
????jsstr += ss
????xx ='''function getcss() {
????s = "";
????for(x in document.styleSheets){
????????for (j in document.styleSheets[x]){
????????????//if(document.styleSheets[x][j] instanceof CSSRuleList){
????????????xx = document.styleSheets[x][j];
????????????for(ss in xx){
????????????????s += xx[ss].cssText;
????????????}
????????????//}
????????}????
????}
????return s;
????}'''
????jsstr += xx
????jsexe = execjs.compile(jsstr, cwd='./node_modules')
????cssstr = jsexe.call("getcss")
????print(cssstr)
解決字體css替換問(wèn)題
需要安裝node.js
導(dǎo)入jsdom模塊到當(dāng)前項(xiàng)目目錄下,用來(lái)運(yùn)行js代碼,獲取生成的css代碼