水平有限,仅供参考。



如图所示,汽车之家的车辆详情里的数据做了反爬对策,数据被CSS伪类替换。



观察 Sources 发现数据就在当前页面。



发现若干条进行CSS替换的js



继续深入此JS



知道了数据与规则,剩下的交给PYTHON。


def repliceStr(browser,textJs,Js): _config_pos=textJs.find("return '.")
JsSplit=textJs[_config_pos:_config_pos+70] ClassHead=re.search("[A-Za-z]{2}_[A-Za-z]{2}",JsSplit,re.S).group(0)
ClassFoot=re.search("_[A-Za-z]+';",JsSplit,re.S).group(0)[:-2] pos=textJs.find("$InsertRule$($index$, $temp$);")
top=textJs[0:pos]
bottom=textJs[pos:] JsBack = browser.execute_script(''' var suc={}; '''+top+' suc[$index$]=$temp$; '+bottom+''' return suc ''') for cj in range(0,len(JsBack)): cjclass=ClassHead+str(cj)+ClassFoot
Js=re.sub("<span class='"+cjclass+"'></span>",JsBack[str(cj)], Js) return Js #获取详情页的数据
def getDetail(base_url): chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get(base_url) #在这里进行文件替换,反爬页面
#keyLink = re.search("var keyLink = (.*?)}];", browser.page_source, re.S)
config = re.search("var config = (.*?)};", browser.page_source, re.S)
option = re.search("var option = (.*?)};", browser.page_source, re.S) option=option.group(0)[13:-1]
config=config.group(0)[13:-1] script = re.findall("<script>(.*?)</script>", browser.page_source,re.S) for i in range(0,len(script)): if re.search("_config",script[i],re.S) : config=repliceStr(browser,script[i],config) if re.search("_option",script[i],re.S) : option=repliceStr(browser,script[i],option) option=demjson.decode(option);
config=demjson.decode(config); carr={}
for item in config['result']['paramtypeitems'] :
bigTitle=item['name']
carr[bigTitle]={}
index=0
for ni in item['paramitems']: cate =ni['name']
value=ni['valueitems'][0]['value']
carr[bigTitle][index]=[cate,value] index=index+1 for item in option['result']['configtypeitems'] :
bigTitle=item['name']
carr[bigTitle]={}
index=0
for ni in item['configitems']: cate =ni['name']
value=ni['valueitems'][0]['value']
carr[bigTitle][index]=[cate,value] index=index+1 time.sleep(10) return carr

刚接触PYTHON,还在学习中,找不到更好的解法。

最新文章

  1. iOS--xuer(registration)
  2. 为什么GOF的23种设计模式里面没有MVC?
  3. 4. 如何解释dalvik字节码
  4. css3属性(一)
  5. thinkphp的mvc理解
  6. 《JAVA与模式》之策略模式
  7. static初始化问题探究
  8. 正则表达式_删除字符串中的任意空格(Regex)
  9. HOWTO: Be more productive
  10. CodeForces 610B Vika and Squares
  11. Sublime Text 3(3207)安装
  12. 两种语言实现设计模式(C++和Java)(一:工厂模式)
  13. #012python实验课
  14. Class AtomicInteger
  15. 哈希函数(hash函数)
  16. NPOI之Excel——设置单元格背景色
  17. react-native android打包
  18. 【详解】JNI (Java Native Interface) (三)
  19. [转](SQL Server) Convert a File from utf-8 to ANSI (such as Windows-1252)
  20. [转]centos6 与 7 其中的一些区别

热门文章

  1. 技术总监的故事告诉大家,要学会say【NO!】
  2. 9.mysql性能优化-慢查询分析、优化索引和配置
  3. Round 4
  4. C# 集合类(二)
  5. Statistical physics approaches to the complex Earth system(相关系统建模理念方法的摘要)
  6. php判断用户设备类型
  7. 解决IE下readonly的Input光标选中的问题
  8. JS控制Video播放器(快进、后退、播放、暂停、音量大小)
  9. C# 9.0新特性详解系列之一:只初始化设置器(init only setter)
  10. K尾相等数(模运算)