day_03 1

 今日内容:
一 Selenium剩余部分
二 BeautifulSoup4

一 Selenium剩余部分
1.元素交互操作:
 ——点击、清除
 ——Actions Chains
 ——frame的切换
 ——执行js代码
from selenium import  webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time

driver=webdriver.Chrome(r'C:\Users\Administrator\Desktop\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('iframeResult')
    time.sleep(1)
    source=driver.find_element_by_id('draggable')
    target=driver.find_element_by_id('droppable')
    print(source.size)
    print(source.tag_name)
    print(source.text)
    print(source.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=2).perform()
        s+=2
        time.sleep(0.1)
    ActionChains(driver).release().perform()
    time.sleep(10)
finally:
    driver.close()

 

 

 

淘宝自动搜索

from selenium import  webdriver
from selenium.webdriver.common.keys import Keys
import time
def get_good(driver):
    num=1
    try:
        time.sleep(5)
        js_code='''
            window.scrollTo(0,5000)
        '''
        driver.execute_script(js_code)
        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
            good_url = good.find_element_by_css_selector('.p-name a').get_attribute('href')
            good_price = good.find_element_by_class_name('p-price').text
            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.txt','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'C:\Users\Administrator\Desktop\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()

 

转载于:https://www.cnblogs.com/ZHKsuika/p/11125376.html

你可能感兴趣的:(php,python)