day03.1元素交互操作及爬取京东商品信息

from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
import time

def get_good(driver):
    num = 1
    try:
        time.sleep(5)

        # 下拉滑动5000距离
        js_code ='''
            window.scrollTo(0,5000)
        '''
        driver.execute_script(js_code)

        # 等待5秒,待商品数据加载
        time.sleep(5)
        good_list = driver.find_elements_by_class_name('gl-item')
        for good in good_list:
            # 商品名称
            good_name = good.find_element_by_css_selector('.p-name em').text
            # print(good_name)
            # 商品链接
            good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')
            # print(good_url)
            # 商品价格
            good_price = good.find_element_by_class_name('p-price').text
            # print(good_price)
            # 商品评价
            good_commit = good.find_element_by_class_name('p-commit').text

            good_content = f'''
            num:{num}
            商品名称:{good_name}
            商品链接:{good_url}
            商品价格:{good_price}
            商品评价:{good_commit}
            \n
            '''
            print(good_content)

            with open('jd.text','a',encoding='utf-8') as f:
                f.write(good_content)
            num += 1
        print('商品信息写入成功')
        # 找到下一页并点击
        next_tag = driver.find_element_by_class_name('pn-next')
        next_tag.click()

        time.sleep(5)
        # 递归调用函数本身
        get_good(driver)
    finally:
        driver.close()

if __name__ == '__main__':
    driver = webdriver.Chrome(r'E:\phyon\driver\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('墨菲定律')
        input_tag.send_keys(Keys.ENTER)

        # 调用获取商品信息函数
        get_good(driver)
    finally:
        driver.close()
from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver import ActionChains  # 破解滑动验证码的时候用的 可以拖动图片
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
import time

driver = webdriver.Chrome(r'E:\phyon\driver\chromedriver.exe')
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()
    # 新方法
    driver.switch_to.frame('iframeResult')
    time.sleep(1)

    # 起始方块id: draggable
    source = driver.find_element_by_id('draggable')
    # 目标方块id: droppable
    target = driver.find_element_by_id('droppable')

    print(source.size)  # 大小
    print(source.tag_name)   #标签名
    print(source.text)    #文本
    print(source.location)   #坐标:X与Y轴

    # 找到滑动距离
    distance = target.location['x'] - source.location['x']
    # 摁住起始滑块
    ActionChains(driver).click_and_hold(source).perform()

    s = 0
    while s < distance:
        # 获取动作链对象,每次移动s距离
        ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
        s += 2
        time.sleep(0.05)

    # 松开起始滑块
    ActionChains(driver).release().perform()
    time.sleep(10)
finally:
    driver.close()
from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver import ActionChains  # 破解滑动验证码的时候用的 可以拖动图片
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
import time

driver = webdriver.Chrome(r'E:\phyon\driver\chromedriver.exe')

try:
    driver.implicitly_wait(10)
    driver.get('https://www.jd.com/')
    time.sleep(5)

    # 点击、清除
    input = driver.find_element_by_id('key')
    input.send_keys('围城')

    # 通过class查找搜索按钮
    search = driver.find_element_by_class_name('button')
    search.click()

    time.sleep(3)

    input2 = driver.find_element_by_id('key')
    input2.clear()

    time.sleep(2)

    input2.send_keys('墨菲定律')
    input2.send_keys(Keys.ENTER)
    time.sleep(10)

finally:
    driver.close()

 

转载于:https://www.cnblogs.com/963989822cmd/p/11125347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值