Crawler Dynamic HTML processing (selenium and PHANTOMJS) executing JavaScript statements

Source: Internet
Author: User

Executing JavaScript statements

1. Hide Baidu Pictures


From selenium import Webdriver
Import time

Driver = Webdriver. PHANTOMJS ()
Driver.get ("https://www.baidu.com/")

# JavaScript script to search for the input box red
JS = "var Q=document.getelementbyid (\" kw\ "); q.style.border=\" 2px solid red\ ";"

# Call to search input box labeled Red JS Script
Driver.execute_script (JS)

#查看页面快照
Driver.save_screenshot ("Redbaidu.png")

#js隐藏元素, hide the captured picture element
IMG1 = Driver.find_element_by_xpath ("//*[@id = ' lg ']/img")
Driver.execute_script (' $ (arguments[0]). FadeOut () ', IMG1)

#重要 (If you do not wait may cause JS to not load and get the desired page)
Time.sleep (10)

Print (' =========== ')
# # Scroll down to the bottom of the page
Driver.execute_script ("$ ('. Scroll_top ')." Click (function () {$ (' html,body '). Animate ({scrolltop: ' 0px '}, 800);}); ")

#查看页面快照
Driver.save_screenshot ("Nullbaidu.png")

Driver.quit ()

2. Scroll to the bottom of the simulation scroll bar

From selenium import Webdriver
Import time

Driver = Webdriver. PHANTOMJS ()
Driver.get ("https://movie.douban.com/typerank?type_name= plot &type=11&interval_id=100:90&action=")

# Scroll down 10000 pixels
JS = "document.body.scrolltop=10000"
#js = "var q=document.documentelement.scrolltop=10000"
Time.sleep (3)

#查看页面快照
Driver.save_screenshot ("Douban.png")

# Execute JS Statement
Driver.execute_script (JS)
Time.sleep (10)

#查看页面快照
Driver.save_screenshot ("Newdouban.png")

Driver.quit ()

Crawler's Dynamic HTML processing (selenium and PHANTOMJS) executes JavaScript statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.