python爬虫之Selenium

首先解决python中使用selenium调用Firefox缺少geckodriver的问题

geckodriver.exe下载地址 : https://pan.baidu.com/s/10Vy9WH1ZpkvdFmZ3T7aw_w , https://github.com/mozilla/geckodriver/releases

解压完成,然后放到python的安装目录与python.exe在同一目录下

Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。

安装
pip3 install selenium
 优缺点:
        优:无需再自己操作cookie和header
        缺:慢
 依赖驱动:
       Firefox
          https://github.com/mozilla/geckodriver/releases
       Chrome
          http://chromedriver.storage.googleapis.com/index.html

具体使用
#!/usr/bin/env python
# coding:utf-8
import  selenium.webdriver
import  time

driver = selenium.webdriver.Firefox()
url = "http://www.baidu.com"
driver.get(url)
driver.find_element_by_class_name("s_ipt").send_keys("你好")

driver.find_element_by_id("su").click()

 selenium的具体使用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。
    安装:
        pip3 install selenium
    优缺点:
        优:无需再自己操作cookie和header
        缺:慢
    依赖驱动:
        Firefox
            https://github.com/mozilla/geckodriver/releases
        Chrome
            http://chromedriver.storage.googleapis.com/index.html
"""

from selenium import webdriver

# 配置驱动
option = webdriver.ChromeOptions()
driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver', chrome_options=option)

# 1. 控制浏览器打开指定页面
driver.get("https://dig.chouti.com/all/hot/recent/1")


# 2. 找到登录按钮
btn_login = driver.find_element_by_xpath('//*[@id="login-link-a"]')
# 3. 点击按钮
btn_login.click()

# 4. 找到手机标签
input_user = driver.find_element_by_xpath('//*[@id="mobile"]')
# 5. 找到密码标签
input_pwd = driver.find_element_by_xpath('//*[@id="mbpwd"]')

# 6. 输入用户名
input_user.send_keys('13121758648')
# 7. 输入密码
input_pwd.send_keys('woshiniba')


# 8. 点击登录按钮
input_submit = driver.find_element_by_xpath(
    '//*[@id="footer-band"]/div[5]/div/div/div[1]/div[2]/div[4]/div[2]/div/span[1]')
input_submit.click()

print(driver.get_cookies())

# 9. 点击跳转
news = driver.find_element_by_xpath('//*[@id="newsContent20646261"]/div[1]/a[1]')
# news.click()
driver.execute_script("arguments[0].click();", news)

# 10.管理浏览器
driver.close()
具体使用

 

 

 


posted @ 2019-02-27 23:10  离人怎挽_wdj  阅读(181)  评论(0编辑  收藏  举报