一、Selenium
1. 元素交互操作
- 点击、清除
- Actions、Chains
- 动作链对象,需要把driver驱动传给它
- 动作链对象可以操作一系列设定好的动作行为 - frame的切换
- switch_to.frame() - 执行js代码
- execute_script()
2. 代码实现
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('chromedriver.exe')
def slowly_move():
try:
driver.implicitly_wait(10)
driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
time.sleep(5)
driver.switch_to.frame('iframeResult')
source = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('droppable')
print('起始:')
print('大小:' + str(source.size), '\n标签名:' + str(source.tag_name), '\n文本:' + str(source.text),
'\n坐标:' + str(source.location))
print('-' * 50)
print('目标:')
print('大小:' + str(target.size), '\n标签名:' + str(target.tag_name), '\n文本:' + str(target.text),
'\n坐标:' + str(target.location))
distance = target.location['x'] - source.location['x']
ActionChains(driver).click_and_hold(source).perform()
s = 0
while s < distance:
ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
s += 2
time.sleep(0.1)
ActionChains(driver).release().perform()
time.sleep(10)
finally:
driver.close()
def quick_move():
driver.implicitly_wait(10)
driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
time.sleep(5)
driver.switch_to.frame('iframeResult')
action = ActionChains(driver)
source = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('droppable')
action.drag_and_drop(source, target).perform()
time.sleep(10)
def jd_search():
try:
driver.implicitly_wait(10)
driver.get('https://www.jd.com')
input_tag = driver.find_element_by_id('key')
input_tag.send_keys('围城')
btn = driver.find_element_by_class_name('button')
btn.click()
time.sleep(5)
input_tag2 = driver.find_element_by_id('key')
input_tag2.clear()
time.sleep(2)
input_tag2.send_keys('美的')
input_tag2.send_keys(Keys.ENTER)
time.sleep(10)
finally:
driver.close()
def js_exec():
try:
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
driver.execute_script(
'''
alert('Hello, World!');
'''
)
time.sleep(10)
finally:
driver.close()
if __name__ == '__main__':
js_exec()
二、获取京东商品列表
- 普通版代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('chromedriver.exe')
try:
driver.implicitly_wait(10)
driver.get('https://www.jd.com')
input_tag = driver.find_element_by_id('key')
input_tag.send_keys('抱枕')
time.sleep(1)
input_tag.send_keys(Keys.ENTER)
time.sleep(5)
js_code = '''
//document.documentElement.cTop = 6000;
window.scrollTo(0, 5000);
'''
driver.execute_script(js_code)
time.sleep(6)
good_lists = driver.find_elements_by_class_name('gl-item')
i = 1
for good in good_lists:
good_name = good.find_element_by_css_selector('.p-name em').text
good_name = good_name.replace('\n', '')
good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')
good_price = good.find_element_by_css_selector('.p-price strong').text
good_commit = good.find_element_by_class_name('p-commit').text
good_content = f'''
商品顺序: {i}
商品名称:{good_name}
商品链接:{good_url}
商品价格:{good_price}
商品评价:{good_commit}
'''
print(good_content)
with open('京东抱枕.txt', mode='a', encoding='utf-8') as f:
f.write(good_content)
i += 1
finally:
driver.close()
- 暴力改进版
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def get_good(num, driver):
try:
js_code = '''
//document.documentElement.cTop = 6000;
window.scrollTo(0, 6000);
'''
driver.execute_script(js_code)
time.sleep(6)
good_lists = driver.find_elements_by_class_name('gl-item')
for good in good_lists:
good_name = good.find_element_by_css_selector('.p-name em').text
good_name = good_name.replace('\n', '')
good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')
good_price = good.find_element_by_css_selector('.p-price strong').text
good_commit = good.find_element_by_class_name('p-commit').text
good_content = f'''
商品顺序: {num}
商品名称:{good_name}
商品链接:{good_url}
商品价格:{good_price}
商品评价:{good_commit}
'''
print(good_content)
with open('京东抱枕.txt', mode='a', encoding='utf-8') as f:
f.write(good_content)
print('写入成功')
num += 1
next_tag = driver.find_element_by_class_name('pn-next')
next_tag.click()
time.sleep(5)
get_good(num, driver)
finally:
driver.close()
if __name__ == '__main__':
driver = webdriver.Chrome('chromedriver.exe')
num = 1
try:
driver.implicitly_wait(10)
driver.get('https://www.jd.com')
input_tag = driver.find_element_by_id('key')
input_tag.send_keys('抱枕')
time.sleep(1)
input_tag.send_keys(Keys.ENTER)
time.sleep(5)
get_good(num, driver)
finally:
driver.close()