Day 03 study notes

A Selenium remaining portion

 

1. elements of interaction :

    - Click to clear

        click

        clear

 

    - ActionChains

        Is a chain of action object need driver driving passed it.

        Action chain object can operate a series of actions to set a good behavior.

 

    - iframe switch

        driver.switch_to.frame('iframeResult')

 

    - the implementation of js codes

        execute_script()

from Selenium Import the webdriver
Import Time

Driver = webdriver.Chrome ()
the try :
    driver.implicitly_wait (10)

    driver.get ( 'https://www.baidu.com/' )

    driver.execute_script (
        '' '         Alert ( "Animal College, Zhejiang most cowhide college ")!         '' ' )     the time.sleep (10) a finally :     driver.close ()


    




 

 

Two BeautifulSoup4

    BS4

 

    1. What BeautifulSoup ?

        bs4 is a resolver library, can in some ( resolver ) to help extract the data we want.

 

    2. Why use BS4 ?

        Because it can quickly extract the data content users want through simple syntax.

 

    3. Classification parser

        - lxml

        - html.parser

 

    4. Installation and Use

        - traverse the document tree

        - Search document tree

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="sister"><b>$37</b></p>
<p class="story" id="p">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" >Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'lxml')
print(soup)
print(type(soup))
html = soup.prettify()
print(html)

 

 

 

Supplementary knowledge :

 

    Data formats :

 

    json data :

    {

    "name": "tank"

    }

 

    XML data :

    <name>tank</name>

 

    HTML:

    <html></html>

 

    Builder : yield value (the value into the builder)

    def f():

 

        # return 1

        yield 1

        yield 2

        yield 3

 

    g = f()

    print(g)

 

    for line in g:

        print(line)

 

 

 

html_doc = """
<html><head><title>The Dormouse's story</title></head><body><p class="sister"><b>$37</b></p><p class="story" id="p">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" >Elsie</a><a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>and they lived at the bottom of a well.</p><p class="story">...</p>
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, 'lxml')
p = soup.find(name='p')
p_s = soup.find_all(name='p')
print(p)
print(p_s)
p = soup.find(name='p', attrs={"id": "p"})
print(p)
tag = soup.find(name='title', text="The Dormouse's story")
print(tag)
tag = soup.find(name='a', attrs={"class": "sister"}, text="Elsie")
print(tag)


import re
a = soup.find(name=re.compile('a'))
print(a)
a_s = soup.find_all(name=re.compile('a'))
print(a_s)

a = soup.find(attrs={"id": re.compile('link')})
print(a)
print(soup.find(name=['a', 'p', 'html', re.compile('a')]))
print(soup.find_all(name=['a', 'p', 'html', re.compile('a')]))
print(soup.find(name=True, attrs={"id": True}))
def have_id_not_class(tag):
    if tag.name == 'p' and tag.has_attr("id") and not tag.has_attr("class"):
        return tag
print(soup.find_all(name=have_id_not_class))
a = soup.find(id='link2')
print(a)
p = soup.find(class_='sister')
print(p)

Guess you like

Origin www.cnblogs.com/xtx642/p/11127941.html