使用selenium爬i车商的车主信息

1.刚开始写博客有不好的地方多多包涵,废话不多说我们进入主题,我们打开网站(https://ics.autohome.com.cn/passport/account/login)这个网站是要先登录才能看到数据的,所以我们要先解决登录,在页面中鼠标右击页面选着查看元素(我使用的是火狐浏览器)
使用selenium爬i车商的车主信息_第1张图片
然后再单击网络,再故意输错账号和密码和验证码,就可以看到会出现两个请求,一个post一个get,post是提交表单,get是验证码链接,点击post请求查看参数,可以看到PasswordDealer这个参数是进行加密了的,这次我是用selenium做的,就不用管他的加密,后面我也会说到怎么解决加密,这个密码是使用rsa加密的有兴趣的可以找他的rsa.js的这个文件如下图
使用selenium爬i车商的车主信息_第2张图片
使用selenium爬i车商的车主信息_第3张图片
使用selenium要自己下载,python是不自带的,可以 pip install selenium下载,下载好还要安装跟浏览器相对应的驱动链接:https://pan.baidu.com/s/1au4-YdHofFZLCb8D2gx9Ow 密码:ot59 这里面有火狐,谷歌,幽灵这三个浏览器的驱动,下载驱动放到python.exe文件的同级目录下就行,好了我们开始撸代码,

from selenium import webdriver
import codecs
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from yz_code import picture_to_validate
from yz_code import md5
import time
from PIL import Image
driver = webdriver.Firefox()

driver.get('https://ics.autohome.com.cn/passport/account/login')
# 所有用户列表
users = [(u'你的账号', '你的密码')]

for u_info in users:
    try:
        # 在一定时间内查找某个标签,如果该标签出现,则结束等待,如果一直不出现,就超时
        element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.ID,'imgValidCodeDealer')))
        # 取出单个用户信息登陆
        driver.find_element_by_id('UserNameDealer').send_keys(u_info[0])
        driver.find_element_by_id('PasswordDealer').send_keys(u_info[1])
        src = driver.find_element_by_id('imgValidCodeDealer')
        src_path = src.get_attribute('src')
        # 截图验证码
        file_path = 'imgs/'+md5(str(src_path))+'.jpeg'
        # 截取整张图片
        driver.save_screenshot('screenshot.png')
        # 获取验证码在网页中的位置
        left = src.location['x']
        top = src.location['y']
        right = src.location['x'] + src.size['width']
        bottom = src.location['y'] + src.size['height']
        # 打开整张图,截取验证码位置图片
        im = Image.open('screenshot.png')
        im = im.crop((left, top, right, bottom))
        # 保存截取后的图片
        im.save(file_path)
        print file_path
        # src.screenshot(file_path)
        # 传入截取验证码的路径,上传云打码
        yzm = picture_to_validate(file_path)
        time.sleep(1)
        print yzm
        # 输入验证码
        driver.find_element_by_id('checkCodeDealer').send_keys(yzm)
        # 点击登录
        driver.find_element_by_id('btnDealer').click()
        time.sleep(4)
        # 输出page_source,
        print driver.page_source
    except Exception as e:
        print e
        driver.quit()
    else:
        driver.quit()

这个里面用到的打码平台是若快打码,因为有账号和密码问题打码平台的代码就不贴出来了,i车商我的账号不能使用了,所以只能做到登录,登录后的就不做了
版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/qq_41936702/article/details/81503593

你可能感兴趣的:(使用selenium爬i车商的车主信息)