很大情况下Ajax请求会使用加密参数
示例:Scrape | Movie
模拟Ajax请求的两种方式
Selenium
pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
browser = webdriver.Edge()
try:
browser.get('https://baidu.com')
input = browser.find_element(By.ID, 'kw')
input.send_keys('Python')
input.send_keys(Keys.ENTER)
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "content_left")))
print(browser.current_url)
print(browser.get_cookies())
print(browser.page_source)
finally:
browser.close()
from selenium import webdriver
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.Safari()
from selenium import webdriver
browser = webdriver.Edge()
browser.get("https://www.taobao.com")
print(browser.page_source)
browser.close()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://www.taobao.com")
input_first = browser.find_element(By.ID, "q")
input_second = browser.find_element(By.CSS_SELECTOR, "#q")
input_third = browser.find_element(By.XPATH, "//*[@id=\"q\"]")
print(input_first, input_second, input_third)
browser.close()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://www.taobao.com")
lis = browser.find_elements(By.CSS_SELECTOR, ".service-bd li")
print(lis)
browser.close()
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
browser = webdriver.Edge()
browser.get('https://taobao.com')
input = browser.find_element(By.ID, 'q')
input.send_keys('iPhone')
time.sleep(1)
input.clear()
input.send_keys("iPad")
button = browser.find_element(By.CLASS_NAME, "btn-search")
button.click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
browser = webdriver.Edge()
browser.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
browser.switch_to.frame("iframeResult")
source = browser.find_element(By.CSS_SELECTOR, "#draggable")
target = browser.find_element(By.CSS_SELECTOR, "#droppable")
actions = ActionChains(browser)
actions.drag_and_drop(source, target)
actions.perform()
from selenium import webdriver
browser = webdriver.Edge()
browser.get("https://www.zhihu.com/explore")
browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
browser.execute_script("alert(\"To Bottom\")")
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://spa2.scrape.center/")
logo = browser.find_element(By.CLASS_NAME, "logo-image")
print(logo)
print(logo.get_attribute("src"))
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://spa2.scrape.center/")
input = browser.find_element(By.CLASS_NAME, "logo-title")
print(input.text)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://spa2.scrape.center/")
input = browser.find_element(By.CLASS_NAME, "logo-title")
print(input.id)
print(input.location)
print(input.tag_name)
print(input.size)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Edge()
browser.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
browser.switch_to.frame("iframeResult")
try:
logo = browser.find_element(By.CLASS_NAME, "logo")
except NoSuchElementException:
print("NO LOGO")
browser.switch_to.parent_frame()
logo = browser.find_element(By.CLASS_NAME, "logo")
print(logo)
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.implicitly_wait(10)
browser.get("https://spa2.scrape.center/")
input = browser.find_element(By.CLASS_NAME, "logo-image")
print(input)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Edge()
browser.get("https://www.taobao.com/")
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, "q")))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn-search")))
print(input, button)
等待条件 | 含义 |
---|---|
title_is | 标题是某内容 |
title_contains | 标题包含某内容 |
presence_of_element_located | 节点出现,参数为节点的定位元组 如(By.ID, “p”) |
visibility_of_element_located | 节点可见,参数为节点的定位元组 |
visibility_of | 可见,参数为节点对象 |
presence_of_all_elements_located | 所有节点都出现 |
text_to_be_present_in_element | 某个节点的文本值中包含某文字 |
text_to_be_present_in_element_value | 某个节点值中包含某文字 |
frame_to_be_available_and_switch_to_it | 加载并切换 |
invisibility_of_element_located | 节点不可见 |
element_to_be_clickable | 按钮可点击 |
staleness_of | 判断一个节点是否仍在DOM中 可知页面是否已经刷新 |
element_to_be_selected | 节点可选择,参数为节点对象 |
element_located_to_be_selected | 节点可选择,参数为节点的定位元组 |
element_selection_state_to_be | 参数为节点对象及状态 相等返回True,否则返回False |
element_located_selection_state_to_be | 参数为定位元组及状态 相等返回True,否则返回False |
alert_is_present | 是否出现警告提示框 |
from selenium import webdriver
import time
browser = webdriver.Edge()
browser.get("https://www.baidu.com/")
browser.get("https://www.taobao.com/")
browser.get("https://www.python.org/")
browser.back()
time.sleep(10)
browser.forward()
browser.close()
from selenium import webdriver
import time
browser = webdriver.Edge()
browser.get("https://www.zhihu.com/explore")
print(browser.get_cookies())
browser.add_cookie({"name": "name", "domain": "www.zhihu.com", "value": "ABC"})
print(browser.get_cookies())
# 如果不sleep可能因为有部分cookie还未加载,导致清空cookies是有cookie残留
time.sleep(5)
browser.delete_all_cookies()
print(browser.get_cookies())
from selenium import webdriver
import time
browser = webdriver.Edge()
browser.get("https://www.baidu.com")
# 开启一个新的选项卡
browser.execute_script("window.open()")
print(browser.window_handles)
browser.switch_to.window(browser.window_handles[1])
browser.get("https://www.taobao.com")
time.sleep(1)
browser.switch_to.window(browser.window_handles[0])
browser.get("https://python.org")
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Edge()
browser.get("https://www.baidu.com")
browser.find_element(By.ID, "hello")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException ,NoSuchElementException
browser = webdriver.Edge()
try:
browser.get("https://www.baidu.com")
except TimeoutException:
print("Time Out")
try:
browser.find_element(By.ID, "hello")
except NoSuchElementException:
print("No Element")
finally:
browser.close()
from selenium import webdriver
browser = webdriver.Edge()
browser.get("https://antispider1.scrape.center/")
from selenium import webdriver
browser = webdriver.Edge()
browser.get("https://antispider1.scrape.center/")
browser.execute_script("Object.defineProperty(navigator, \"webdriver\", {get: () => undefined})")
from selenium import webdriver
from selenium.webdriver import EdgeOptions
option = EdgeOptions()
# 隐藏提示条
option.add_experimental_option("excludeSwitches", ["enable-automation"])
# 隐藏自动化扩展信息
option.add_experimental_option("useAutomationExtension", False)
browser = webdriver.Edge(options=option)
browser.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument", {
"source": "Object.defineProperty(navigator, \"webdriver\", {get: () => undefined})"})
browser.get("https://antispider1.scrape.center/")
from selenium import webdriver
from selenium.webdriver import EdgeOptions
option = EdgeOptions()
option.add_argument("--headless")
browser = webdriver.Edge(options=option)
browser.set_window_size(1500, 800)
browser.get("https://www.baidu.com")
browser.get_screenshot_as_file("preview.png")