水一篇文章,记录下近来忘记的小技巧

1.JS 三元表达式

前置条件 ? 结果一 : 结果二;

不管写成什么格式,都按照这样去看

2.xpath小语法

res= requests.get('url').text
html = etree.HTML(res)
# 取出包含文章主体
content= html.xpath('//*body[@id="aaa"]')[0]
text_one= etree.tostring(content, method='html')
text_two= HTMLParser().unescape(text_one.decode())
print(text_two)
text= response.xpath('//*span[@class="newText"]').xpath('string(.)')[0]
text= response.xpath('string(//*span[@class="newText"])')

3.pyexecjs

import execjs

with open('tj.js','r',encoding='utf-8') as f:
    f_js = f.read()
f_js = execjs.compile(f_js.encode().decode('gbk','ignore'))
link_name = f_js.call('get_PWD','123456')

 

posted @ 2020-12-03 17:22  黑山老道  阅读(160)  评论(0编辑  收藏  举报