1 js2py简介
1.1 js2py模块使⽤
Python中执⾏JS代码,通常两个库:js2py,pyexecjs
js2py是纯python实现的库,⽤于在python中运⾏js代码,本质上是将js代码翻
译成python代码
js2py安装 pip install js2py
cmd里面 安装就行
1.2 快速⼊⻔
# http://www.porters.vip/verify/sign/#
import js2py
# js2py.eval_js('console.log("hello world")')
#
# func_js = """
# function add(a,b){
# return a+b
# }
# """
#
# add = js2py.eval_js(func_js)
# print(add(1, 2))
print(js2py.eval_js('var a = "python";a'))
add = js2py.eval_js('function add(a,b){return a + b}')
print(add(2,3))
结果
python
5
js代码翻译
import js2py
print(js2py.translate_js("console.log('hello world')"))
结果
from js2py.pyjs import *
# setting scope
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([])
var.get('console').callprop('log', Js('hello world'))
将将js⽂件翻译为Python脚本
# 将js⽂件翻译为Python脚本
js2py.translate_file('test.js', 'test.py')
js代码中使⽤函数
# lst = [1,2,3]
# r = sum(lst)
# print(r)
import js2py # 导入了一个模块
print('sum:',sum([1,2,3])) # 执行了一个Python代码
context = js2py.EvalJs({'python_sum':sum}) # 定义了一个python_sum函数
js_code = '''
python_sum([1,2,3]) # 通过js_code把值传进来
'''
print('js_code:',context.eval(js_code))
结果
sum: 6
js_code: 6
import js2py # 导入了一个模块
# 在js代码中导⼊Python模块并使⽤
# 使⽤pyimport语法
js_code = """
pyimport requests
console.log('导⼊成功');
var response = requests.get('http://www.baidu.com');
console.log(response.url);
console.log(response.content);
"""
js2py.eval_js(js_code)
2 动态HTML技术了解
2.1 爬⾍和反爬⾍的⽃争
- 爬⾍建议
尽量减少请求次数
能抓取列表⻚就不抓详情⻚
保存获取到的HTML,供查错和重复使⽤ - 关注⽹站的所有类型的⻚⾯
H5⻚⾯
APP - 多伪装
代理IP
随机请求头 - 利⽤多线程分布式
在不被发现的情况下我们尽可能的提⾼速度
2.2 ajax基本介绍
动态了解HTML技术
- JS
是⽹络上最常⽤的脚本语⾔,它可以收集⽤户的跟踪数据,不需要重载⻚⾯直接提交表单,在⻚⾯嵌⼊多媒体⽂件,甚⾄运⾏⽹⻚ - jQuery
jQuery是⼀个快速、简介的JavaScript框架,封装了JavaScript常⽤的功能代码 - ajax
ajax可以使⽤⽹⻚实现异步更新,可以在不重新加载整个⽹⻚的情况下,对⽹⻚的某部分进⾏更新
获取ajax数据的⽅式
1.直接分析ajax调⽤的接⼝。然后通过代码请求这个接⼝。
2.使⽤Selenium+chromedriver模拟浏览器⾏为获取数据
3 Selenium+chromedriver获取动态数据
3.1 Selenium 介绍
- selenium是⼀个web的⾃动化测试⼯具,最初是为⽹站⾃动化测试⽽开发的,selenium可以直接运⾏在浏览器上,它⽀持所有主流的浏览器,可以接收指令,让浏览器⾃动加载⻚⾯,获取需要的数据,甚⾄⻚⾯截屏
- chromedriver是⼀个驱动Chrome浏览器的驱动程序,使⽤他才可以驱动浏览器。当然针对不同的浏览器有不同的driver。以下列出了不同浏览器及其对应的driver:
Chrome:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Firefox:https://github.com/mozilla/geckodriver/releases
Edge:https://developer.microsoft.com/en-us/microsoftedge/tools/webdriver/
Safari:https://webkit.org/blog/6900/webdriver-support-insafari-10/ - 下载chromedriver
- 百度搜索:淘宝镜像(https://npm.taobao.org/)
- 安装总结:https://www.jianshu.com/p/a383e8970135
- 安装Selenium:pip install selenium
3.2 Phantomjs快速⼊⻔
⽆头浏览器:⼀个完整的浏览器内核,包括js解析引擎,渲染引擎,请求处理等,但是不包括显示和⽤户交互⻚⾯的浏览器
Phantomjs案例
from selenium import webdriver
# PhantomJS 无界面浏览器
driver = webdriver.PhantomJS(r'D:\Users\RAZER\PycharmProjects\spider\lect08\phantomjs.exe')
# 打开百度
driver.get('https://www.baidu.com/')
# 截屏
driver.save_screenshot('baidu.png')
搜索胡歌
from selenium import webdriver
import time
# PhantomJS 无界面浏览器
driver = webdriver.PhantomJS(r'D:\Users\RAZER\PycharmProjects\spider\lect08\phantomjs.exe')
# 打开百度
driver.get('https://www.baidu.com/')
# 定位操作
driver.find_element_by_id('kw').send_keys('胡歌')
# 百度一下的按钮
driver.find_element_by_id('su').click()
time.sleep(3)
拿到胡歌的搜索页面
from selenium import webdriver
import time
# PhantomJS 无界面浏览器
driver = webdriver.PhantomJS(r'D:\Users\RAZER\PycharmProjects\spider\lect08\phantomjs.exe')
# 打开百度
driver.get('https://www.baidu.com/')
# 定位操作
driver.find_element_by_id('kw').send_keys('胡歌')
# 百度一下的按钮
driver.find_element_by_id('su').click()
# time.sleep(3)
# 查看网页源代码
# print(driver.page_source)
# 查看当前请求的url地址
print(driver.current_url)
# 截屏
# driver.save_screenshot('baidu.png')
结果
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E8%83%A1%E6%AD%8C&fenlei=256&rsv_pq=f3c4202100054f79&rsv_t=f036GPh62h4HDccXWIeOsbmOjMIBiyvIonxukSbJKS7CUPUBSjvukJCPArk&rqlang=cn&rsv_enter=0&rsv_dl=ib&rsv_sug3=2&rsv_btype=i&inputT=63&rsv_sug4=63
# 4.退出
driver.quit()
3.3 selenium快速⼊⻔
from selenium import webdriver
import time
driver = webdriver.Chrome()
# 打开百度
driver.get('https://www.baidu.com/')
# 窗口最大化
driver.maximize_window()
# 等待3秒
time.sleep(3)
# 窗口最小化
driver.minimize_window()
# 退出浏览器
# driver.quit()
# 关闭窗口
# driver.close()