python-webbrowser/requests/BeautifulSoup/selenium

webbrowser

webbrowser.open()#用于启动浏览器打开一个网页

requests

1.第三方模块,需要安装:pip install requests

2.作用:从web下载东西

3.requests.get() :下载一个网页

    


将res写入文件时要用二进制模式打开文件(为了板胡该文本中的unicode编码)

        f = open('lz.txt','wb')

        for chunk in res.iter_content(100000):#iter_content:返回指定长度的内容

        f.write(chunk)

BesutifulSoup

1.第三方模块,需要安装:pip install besutifulsoup4

2.导入时 使用 import bs4

3.创建BesutifulSoup对象

        (1)从网络上下载网页

                >>> import bs4

                >>> import requests

                >>> res = requests.get('https://www.baidu.com')
                >>> res.raise_for_status()

                >>> bea = bs4.BeautifulSoup(res.text)

         (2)从硬盘加载html文件

                >>> exampleFile = open('example.html')

                >>> examplesoup = bs4.BeautifulSoup(exampleFile)

4.select()#寻找元素

            >>> elems = examplesoup.select('#author')
            >>> type(elems)
            <class 'list'>
            >>> elems[0].getText()
            'Al Sweigart'
            >>> str(elems[0])
            '<span id="author">Al Sweigart</span>'
            >>> elems[0].attrs

            {'id': 'author'}

selenium

1.第三方模块,需安装使用

2.导入:from selenium import webdriver

3.eg:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.inventwithpython.com')
try:
    elem = browser.find_element_by_class_name('bookcover')#获取相应内容
    print('Found <%s> element with that class name!'%(elem.tag_name))
except:
    print('not find')


linkElem = browser.find_element_by_link_text('Read Online for Free')#查找指定按钮
linkElem.click()#模拟点击


emailElem = browser.fing_element_by_id('Email')#根据id查找内容
emailElem.send_key('sdsds')#向找到的输入框中添加 内容
emailElem.submit()#点击该元素所在的表单的提交按钮


browser.back()#返回按钮
browser.forward()#前进按钮
browser.refresh()#刷新
browser.quit()#关闭窗口

猜你喜欢

转载自blog.csdn.net/qq_41484460/article/details/80211887
今日推荐