python爬虫全套教程js
Python 爬虫需要解析 Js 代码来获取动态加载的数据。解析方法包括:Webdriver:直接执行 Js 代码。Beautiful Soup:通过 lxml 扩展包解析 Js 代码。Selenium:执行 Js 代码并获取页面信息。
Python 爬虫全套教程:Js 解析
Js 解析概述
Js 是一种脚本语言,用于增强网页的交互性。爬虫需要解析 Js 代码才能获取动态加载的数据。
Js 解析方法
立即学习“Python免费学习笔记(深入)”;
Python 中有多种解析 Js 代码的方法:
使用方法
Webdriver
from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://example.com")result = driver.execute_script("return document.title")
Beautiful Soup
from bs4 import BeautifulSoupsoup = BeautifulSoup(driver.page_source, "lxml")title = soup.select_one("script[type='text/javascript']").text
Selenium
from selenium.webdriver import Chromeoptions = ChromeOptions()options.add_argument("--headless")driver = Chrome(options=options)driver.get("https://example.com")result = driver.execute_script("return document.title")
注意
高级用法