当前位置:网站首页>29. Selenium module is a module of Python operating browser software, which can realize JS dynamic web page request

29. Selenium module is a module of Python operating browser software, which can realize JS dynamic web page request

2020-11-10 18:04:54 A lion in the sky

Baidu cloud search , Search all kinds of information :http://www.lqkweb.com
Search the Internet disk , Search all kinds of information :http://www.swpan.cn

selenium modular

selenium The module is a third party module and needs to be installed ,selenium Module is an operation of various browser corresponding software api Interface module

selenium Module is an operation of various browser corresponding software api Interface module , So you need to download the corresponding browser operating software

The principle of operation is :selenium Module operation browser operation software , Browser operating software operating browser

Selenium 2.0 For the following browsers
  Google Chrome
  Internet Explorer 7, 8, 9, 10, 11
  Firefox
  Safari
  Opera
  HtmlUnit
  phantomjs
  Android
  iOS

image

Selenium At the heart of , Just use js Control browser

Download the browser operating software of the corresponding browser

Chrome: https://sites.google.com/a/ch...
Edge: https://developer.microsoft.c...
Firefox: https://github.com/mozilla/ge...
Safari: https://webkit.org/blog/6900/...

We're going to list it with the Firefox browser

First of all, the operating software of the Firefox browser ,geckodriver.exe The files are placed in the crawler directory

selenium The module can simulate user behavior and operate various versions of browsers

webdriver.Firefox(' Operating browser software path ') Instantiate the Firefox browser object
get('url') Visit website
find_element_by_xpath('xpath expression ') adopt xpath The expression finds the corresponding element
clear() Clear the input box
send_keys(' Content ') Write the content into the input box
click() Click event
get_screenshot_as_file(' Screenshot save path name ') Take a screenshot of the page , Save to this directory
page_source Access to web pages htnl Source code
browser.close()  Close the browser

#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver  #  Import selenium Module to operate browser software 
import time

browser = webdriver.Firefox(executable_path='H:/py/16/adc/adc/Firefox/geckodriver.exe')
browser.get('https://www.tmall.com/?spm=a220o.1000855.a2226mz.1.5c90c3484bZCx6')

#  Simulate user operation 
browser.find_element_by_xpath('//input[@id="mq"]').clear()                 #  adopt xpath The expression finds the input box ,clear() Clear the input box 
browser.find_element_by_xpath('//input[@id="mq"]').send_keys(' dress ')     #  adopt xpath The expression finds the input box ,send_keys() Write the content into the input box 
browser.find_element_by_xpath('//button[@type="submit"]').click()          #  adopt xpath Expression to find the search button ,click() Click event 

time.sleep(3)   #  wait for 3 second 
browser.get_screenshot_as_file('H:/py/17/img/123.jpg')  #  Take a screenshot of the page , Save to this directory 

neir = browser.page_source   #  Get web content 
print(neir)

browser.close()     #  Close the browser 

utilize scrapy Of Selector Method . To filter the selection data

Selector() Method , Filter smart data , The parameter is the resulting string html Source code

#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver  #  Import selenium Module to operate browser software 
import time
from scrapy.selector import Selector

browser = webdriver.Firefox(executable_path='H:/py/16/adc/adc/Firefox/geckodriver.exe')
browser.get('https://www.tmall.com/?spm=a220o.1000855.a2226mz.1.5c90c3484bZCx6')

#  Simulate user operation 
browser.find_element_by_xpath('//input[@id="mq"]').clear()                 #  adopt xpath The expression finds the input box ,clear() Clear the input box 
browser.find_element_by_xpath('//input[@id="mq"]').send_keys(' dress ')     #  adopt xpath The expression finds the input box ,send_keys() Write the content into the input box 
browser.find_element_by_xpath('//button[@type="submit"]').click()          #  adopt xpath Expression to find the search button ,click() Click event 

time.sleep(3)   #  wait for 3 second 
browser.get_screenshot_as_file('H:/py/17/img/123.jpg')  #  Take a screenshot of the page , Save to this directory 

neir = browser.page_source   #  Get web content 
# print(neir)
gl_neir = Selector(text=neir)
dedao = gl_neir.css('title::text').extract()
print(dedao)

browser.close()     #  Close the browser 

selenium Operate the browser scroll bar

execute_script(js) Method , Implement the original ecology js Script

#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver  #  Import selenium Module to operate browser software 
import time
from scrapy.selector import Selector

browser = webdriver.Firefox(executable_path='H:/py/16/adc/adc/Firefox/geckodriver.exe')
browser.get('https://www.oschina.net/blog')

time.sleep(3)       #  wait for 3 second 
for i in range(3):  #  rolling 3 Secondary scroll bar 
    js = 'window.scrollTo(0,document.body.scrollHeight); var lenofpage=document.body.scrollHeight; return lenofpage'
    browser.execute_script(js)  #  perform js Language scroll bar 
    time.sleep(3)

neir = browser.page_source   #  Get web content 
# print(neir)
gl_neir = Selector(text=neir)
dedao = gl_neir.css('title::text').extract()
print(dedao)

# browser.close()     #  Close the browser 

Set request page not to load image , Improve request efficiency
ChromeOptions() Method , Create Google browser settings object
Chrome() Method , Create Google browser objects

Here's Google browser

#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver  #  Import selenium Module to operate browser software 
from scrapy.selector import Selector

# Set request page not to load image , Improve request efficiency 
chrome_options = webdriver.ChromeOptions()                          # Create Google browser settings object 
prefs = {"profile.managed_default_content_settings.images": 2}      # Set Google browser not to load images 
chrome_options.add_experimental_option('prefs', prefs)              # Add no loaded pictures to browser 

browser = webdriver.Chrome(executable_path='H:/py/16/adc/adc/Firefox/chromedriver.exe', chrome_options=chrome_options)
# browser.set_page_load_timeout(40) # Set the maximum loading time of the page to 40s
browser.get('https://www.taobao.com/')

neir = browser.page_source   #  Get web content 
# print(neir)
gl_neir = Selector(text=neir)
dedao = gl_neir.css('title::text').extract()
print(dedao)

# browser.close()     #  Close the browser 

selenium The module can also operate PhantomJS browser ,PhantomJS It's a browser with no interface , It's refreshing , But multithreading can degrade performance

a key : We recommend using chromedriver.exe, Google browser

版权声明
本文为[A lion in the sky]所创,转载请带上原文链接,感谢