汽车之家的口碑不能正常采集了
返回的数据是 json 的,只需要这样解析就可以了
json 的方式只能进行相关的转码操作,并不能解析到数据
内部的数据的解析方式是,通过返回的 js 代码进行解析的#!/usr/bin/env python3# -*- coding: utf-8 -*-# @Date : 2017-03-07 10:06:14# @Author : xchaoinfo (xchaoinfo)# @github : https://github.com/xchaoinfo# @Version : 0.0.0import requestsfrom bs4 import BeautifulSoupua = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"headers = { "Host": "k.autohome.com.cn", "Referer": "http://k.autohome.com.cn/880/", "User-Agent": ua, "X-Requested-With": "XMLHttpRequest"}def autohome(url): html = requests.get(url, headers=headers, timeout=4) # print(html.json()) soup = BeautifulSoup(html.json(), \u0026#39;lxml\u0026#39;) # print(soup.text) span = soup.find_all("span") print(len(span))if __name__ == \u0026#39;__main__\u0026#39;: url = "http://k.autohome.com.cn/FrontAPI/GetFeelingByEvalId?evalId=1491279" autohome(url)
■网友
汽车之家的口碑使用了字体和css替换文本,并且对ip做了限制!神箭手使用独创的解析技术和私密代理ip,完美解决了口碑不能采集的问题。
现在神箭手上已经上架了这个口碑爬虫,感兴趣的童鞋可以了解下:汽车之家口碑采集爬虫-神箭手云
■网友
仔细解释下吧,这是我和同事一块儿完成的,反编译汽车之家前端工程师的JS代码,能看懂,就能临时解决问题。颠覆了我对反爬虫的认识,前端真牛比。。。。(function(document) { function Xw_() { //如果样式sheet支持insertRule if (check_insert_rule()) { //屏蔽getcomputedStyle,设置别名 if (new_getComputedStyle(\u0026#39;sheet_table\u0026#39;) == \u0026#39;sheet_table\u0026#39;) { write_css_in_sheet(); } } if (!check_insert_rule()) { if (new_getComputedStyle(\u0026#39;yN_\u0026#39;) != \u0026#39;style_element\u0026#39;) { write_css_in_html(); } } } function AA_() { if (window.location.href().indexOf(\u0026#39;k.autohome.com.cn\u0026#39;()) \u0026lt; 0) { return \u0026#39;pl\u0026#39;; } word_string = window.decodeURIComponent(\u0026#39;不严个乘低信内准分力动华反合后坐够大头好子实平应心性意慢排撼散明显松步满用的确精胖自舒评豪起适部重震顺饰高\u0026#39;); bi_ = keyword_split((new_getComputedStyle(\u0026#39;\u0026#39;)35,26;19,36;17;44,11;52;41,10;12,23,27;40,20;3,15;18,47;14,28;13,46;10,9;45,34;22,50;31,32,37;4;39,38;19,43;33,30;5,24;7,38;42,46,25;0,16;35,8;6,51;49,29;2,25;21,36;1,48), new_defaultview_getComputedStyle(\u0026#39;;\u0026#39;)); oG_ = keyword_split((6_29,28_0;4_67,30_36;0_91,18_72;33_94,1_92;12_135,6_128;20_163,14_148;4_231,46_184;3_268,31_236;3_304,31_272;11_332,23_308;10_369,24_344;15_400,19_380;19_432,15_416;2_485,32_452;8_515,26_488;7_568,43_524;2_593,16_576;13_618,21_596;10_658,24_632;17_687,17_669;7_733,27_705;13_763,21_741;41_787,9_777;5_859,29_829;3_897,31_865;0_936,34_901;17_955,17_937;9_999,25_973;33_1011,1_1009;2_1078,32_1045 \u0026#39;;\u0026#39;), \u0026#39;;\u0026#39;); Xw_(); return \u0026#39;;\u0026#39;; } var oG_ = \u0026#39;\u0026#39;; var bi_ = \u0026#39;\u0026#39;; var word_string = \u0026#39;\u0026#39;; var style_element = document.createElement(\u0026#39;style\u0026#39;); if (document.head) { document.head.appendChild(style_element); } else { document.getElementsByTagName(\u0026#39;head\u0026#39;).appendChild(style_element); } var sheet_table = style_element.sheet; function check_insert_rule() { if (sheet_table !== undefined \u0026amp;\u0026amp; sheet_table) { return true; } else { return false; } } //支持sheet的inertRule, function write_css_in_sheet() { //如果不是口碑頁面,則返回pl, 如果是則進行一番不可描述的操作 if (window.location.href().indexOf(\u0026#39;k.autohome.com.cn\u0026#39;()) \u0026lt; 0) { return \u0026#39;pl\u0026#39;; } for (replace_index = 0; replace_index \u0026lt; bi_.length; replace_index++) { var ZA_ = keyword_split(bi_, \u0026#39;,\u0026#39;); var replaced_str = \u0026#39;\u0026#39;; for (NX_ = 0; NX_ \u0026lt; ZA_.length; NX_++) { replaced_str += word_string.charAt(ZA_)\u0026#39;\u0026#39;; } process_single_css_sheet(replace_index, replaced_str); } } // 生成一个 CSS类的before content的值,插入样式规则到内存 function process_single_css_sheet(replace_index, replaced_str) { sheet_table.insertRule(get_css_name(replace_index)\u0026#39;::before { content:"\u0026#39;replaced_str\u0026#39;" }\u0026#39;, 0); var ZA_ = document.querySelectorAll(get_css_name(replace_index)); //置空新增节点的所有样式,字体大小,颜色之类的继承自父节点 for (x in ZA_) { try { ZA_.currentStyle = \u0026#39;\u0026#39;; } catch (e) {} } } // 生成CSS 的类名称 function get_css_name(replace_index) { return \u0026#39;._kw\u0026#39;replace_index\u0026#39;_mainpl\u0026#39;; } function aC_() { return \u0026#39;::before { content:\u0026#39; } //不支持sheet的inertRule, 直接寫html代碼 function write_css_in_html() { if (window.location.href().indexOf(\u0026#39;k.autohome.com.cn\u0026#39;()) \u0026lt; 0) { return \u0026#39;pl\u0026#39;; } var wk_ = get_custom_style(); for (index = 0; index \u0026lt; oG_.length; index++) { var ZA_ = keyword_split(oG_, \u0026#39;,\u0026#39;); var Vz_ = \u0026#39;\u0026lt;span class="_k6complex _9mainpl"\u0026gt;\u0026#39;; for (NX_ = 0; NX_ \u0026lt; ZA_.length; NX_++) { var nY_ = keyword_split(ZA_, \u0026#39;_\u0026#39;); Vz_ += \u0026#39;\u0026lt;span class="_kwd" style="width:\u0026#39;nY_\u0026#39;px;\u0026#39;wk_\u0026#39;"\u0026gt;\u0026lt;span class="_img" style="margin-left:-\u0026#39;nY_\u0026#39;px;"\u0026gt;\u0026lt;/span\u0026gt;\u0026lt;/span\u0026gt;\u0026#39;; } Vz_ += \u0026#39;\u0026lt;/span\u0026gt;\u0026#39;; process_single_css(index, Vz_); } } function eS_() { if (window.location.href().indexOf(\u0026#39;k.autohome.com.cn\u0026#39;()) \u0026lt; 0) { return \u0026#39;pl\u0026#39;; } var wk_ = get_custom_style(); for (index = 0; index \u0026lt; oG_.length; index++) { if (index % 2 == 0) { var ZA_ = keyword_split(oG_, \u0026#39;,\u0026#39;); var Vz_ = \u0026#39;\u0026lt;span class="_k6complex _9mainpl"\u0026gt;\u0026#39;; for (NX_ = 0; NX_ \u0026lt; ZA_.length; NX_++) { var nY_ = keyword_split(ZA_, \u0026#39;_\u0026#39;); Vz_ += \u0026#39;\u0026lt;span class="_kwd" style="width:\u0026#39;nY_\u0026#39;px;\u0026#39;wk_\u0026#39;"\u0026gt;\u0026lt;span class="_img" style="margin-left:-\u0026#39;nY_\u0026#39;px;"\u0026gt;\u0026lt;/span\u0026gt;\u0026lt;/span\u0026gt;\u0026#39;; } Vz_ += \u0026#39;\u0026lt;/span\u0026gt;\u0026#39;; process_single_css(index, Vz_); } else { var ZA_ = keyword_split(bi_, \u0026#39;,\u0026#39;); var Vz_ = \u0026#39;\u0026#39;; for (NX_ = 0; NX_ \u0026lt; ZA_.length; NX_++) { Vz_ += word_string.charAt(ZA_)\u0026#39;\u0026#39;; } process_single_css_sheet(index, Vz_); } } } // 获取自定义样式 function get_custom_style() { var wk_ = \u0026#39;\u0026#39;; try { if (HS_GetCustomStyle) { wk_ = HS_GetCustomStyle(); } else { if (navigator.userAgent.indexOf(\u0026#39;Windows NT 5\u0026#39;) != -1) { wk_ = \u0026#39;margin-bottom:-4.8px;\u0026#39;; } else { wk_ = \u0026#39;margin-bottom:-5px;\u0026#39;; } } } catch (e) {} return wk_; } function process_single_css_innerhtml(Is_, yN_) { //查询某一个CSS类的所有节点 var ZA_ = document.querySelectorAll(get_css_name(Is_)); //遍历节点,设置节点内容 for (x in ZA_) { ZA_.innerHTML = yN_; try { ZA_.currentStyle = \u0026#39;\u0026#39;; } catch (e) {} } } //将一个字符串根据分隔符切分成列表 function keyword_split(Is_, splitter) { if (Is_) { return Is_.split(splitter); } else { return \u0026#39;\u0026#39;; } } // 根据字符的word_string中的位置,获取相应的字符 function DX_(Is_) { return word_string.charAt(parseInt(Is_)); } function new_defaultview_getComputedStyle(Is_) { set_computed_style_alias(); if (document.defaultView) {\t\t\t// getComputedStyle document.defaultView.getComputedStyle = function(element, pseudoElt) { if (pseudoElt != undefined \u0026amp;\u0026amp; typeof (pseudoElt) == \u0026#39;string\u0026#39; \u0026amp;\u0026amp; pseudoElt.toLowerCase().indexOf(\u0026#39;:before\u0026#39;) \u0026gt; -1) { var obj = {}; obj.getPropertyValue = https://www.zhihu.com/api/v4/questions/56527048/function(x) { return x; } ; return obj; } else { return window._fuckyou_dd(element, pseudoElt); } } ; } return Is_; } function new_getComputedStyle(Is_) { set_computed_style_alias(); window.getComputedStyle = function(element, pseudoElt) { if (pseudoElt != undefined /u0026amp;/u0026amp; typeof (pseudoElt) == /u0026#39;string/u0026#39; /u0026amp;/u0026amp; pseudoElt.toLowerCase().indexOf(/u0026#39;:before/u0026#39;) /u0026gt; -1) { var obj = {}; obj.getPropertyValue = function(x) { return x; }; return obj; } else { return window._fuckyou(element, pseudoElt); } }; return Is_; } function set_computed_style_alias() { if (window._fuckyou == undefined) { window._fuckyou = window; } // 如果存在defaultView 属性,则设置defaultView的getcomputedStyle为fuckyou_dd if (document.defaultView) { if (window._fuckyou_dd == undefined) { window._fuckyou_dd = document.defaultView.getComputedStyle; } } }})(document);
推荐阅读
- 铜仁■江苏苏州:“铜仁之家”的“温暖异乡年”
- 南京■南京长途汽车东站12月28日起恢复发车
- 子良说汽车|崔克“唯一”的车型?TREK Domane进化史
- 汽车|马自达全新CX-5曝光!搭直列六缸引擎/后驱平台
- 汽车|8款新车12月份上市,开回家过年很有面子
- 汽车|9万不到的SUV 居然有197马力 而且还有大空间7座!
- 汽车|续航里程410km 华晨新日新能源汽车首车下线
- 汽车|评测 | 懂你所想,予你所需!依维柯欧胜2021款这个“搭档”可还行?
- 汽车知识|凯美瑞2.0L落地近22万,动力差,优惠少,但就是销量高!
- 汽车市场|品牌与性价比亦可兼得 实拍奥迪Q2L
