07 selenium的使用

selenium

为了解决requests无法直接执行JavaScript代码的问题

安装:

pip3 install selenium

浏览器的驱动:

ps:需要下载对应版本的浏览器

http://npm.taobao.org/mirrors/chromedriver/

84.0.4147.105:驱动用84.0.4147.30

一、打开百度

from selenium import webdriver
import time
# 指定使用跟那个驱动 这里直接放在了根目录下
bro=webdriver.Chrome(executable_path='./chromedriver.exe') # 得到一个谷歌浏览器对象,
time.sleep(2) # 有时候会有网络延迟等等因素(后面有隐式等待...)
bro.get('https://www.baidu.com/')  # 在地址栏里输入了百度
time.sleep(2)
print(bro.page_source)
time.sleep(2)
bro.close() # 一定要关闭掉

二、选择器(find方法)

1、find_element_by_id   # 通过id查找控件
2、find_element_by_link_text  # 通过a标签内容找
3、find_element_by_partial_link_text  # 通过a标签内容找,模糊匹配
4、find_element_by_tag_name   # 标签名
5、find_element_by_class_name  # 类名
6、find_element_by_name      # name属性
7、find_element_by_css_selector  # 通过css选择器
8、find_element_by_xpath       # 通过xpaht选择器
# elements和element
1、find_elements_by_xxx的形式是查找到多个元素,结果为列表

三、获取元素属性(常用)

tag.get_attribute('href')  # 找当前控件 的href属性对的值
tag.text   # 获取文本内容
# 了解
# print(tag.id)   # 当前控件id号
# print(tag.location)  # 当前控件在页面位置
# print(tag.tag_name)  # 标签名
# print(tag.size)      #标签的大小

四、无界面浏览器(phantomjs)

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度

chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
bro=webdriver.Chrome(chrome_options=chrome_options,executable_path='./chromedriver.exe')
bro.get('https://www.baidu.com/')
print(bro.page_source)
bro.close()

五、元素交互(常用)

tag.send_keys()  # 往里面写内容
tag.click()      # 点击控件
tag.clear()      # 清空控件内容

六、执行js

from selenium import webdriver
import time
bro=webdriver.Chrome(executable_path='./chromedriver.exe')
bro.implicitly_wait(5)  # 隐士等待:找一个控件,如果控件没有加载出来,等待5s中  等待所有,只需要写着一句,以后找所有控件都按这个操作来
bro.get('https://www.baidu.com/')


bro.execute_script('window.open()')
bro.execute_script('window.open()')
time.sleep(2)
bro.close()

七、模拟浏览器的前进与后退

from selenium import webdriver
import time
browser=webdriver.Chrome(executable_path='./chromedriver.exe')
browser.get('https://www.baidu.com')
browser.get('https://www.taobao.com')
browser.get('http://www.sina.com.cn/')

browser.back()
time.sleep(1)
browser.forward()

browser.close()

爬取京东商品信息

from selenium import webdriver
import time
# 模拟键盘输入
from selenium.webdriver.common.keys import Keys
bro=webdriver.Chrome(executable_path='./chromedriver.exe')
# 设置隐士等待
bro.implicitly_wait(10)

def get_goods_info(bro):
    # li_list=bro.find_element_by_class_name('gl-warp').find_elements_by_tag_name('li')
    # goods=bro.find_elements_by_class_name('gl-item')
    goods = bro.find_elements_by_css_selector('.gl-item')
    # print(len(goods))
    for good in goods:
        try:
            price = good.find_element_by_css_selector('.p-price i').text
            name = good.find_element_by_css_selector('.p-name em').text
            url = good.find_element_by_css_selector('.p-img a').get_attribute('href')
            commits = good.find_element_by_css_selector('.p-commit strong>a').text
            photo_url = good.find_element_by_css_selector('.p-img img').get_attribute('src')

            print('''
            商品名字:%s
            商品价格:%s
            商品地址:%s
            商品评论数:%s
            商品图片地址:%s
    
            ''' % (name, price, url, commits, photo_url))
        except Exception as e:
            continue

    next_button = bro.find_element_by_partial_link_text('下一页')
    time.sleep(1)
    next_button.click()

    get_goods_info(bro)

try:
    bro.get('https://www.jd.com/')

    input_k=bro.find_element_by_id('key')

    input_k.send_keys('奶牛')
    # 模拟键盘的回车键
    input_k.send_keys(Keys.ENTER)
    get_goods_info(bro)


except Exception as e:
    print(e)

finally:
    bro.close()
posted @ 2020-08-05 16:14  江湖有梦  阅读(192)  评论(0编辑  收藏  举报