wordpress怎么更换站点,小型求职招聘网站源码 php,给公司建立网站,08wordpress拷贝地址#xff1a;python selenium爬虫自动登录实例_python selenium登录_Ustiniano的博客-CSDN博客 一、概述
我们要先安装selenium这个库#xff0c;使用pip install selenium 命令安装#xff0c;selenium这个库相当于机器模仿人的行为去点击浏览器上的元素#xff0…拷贝地址python selenium爬虫自动登录实例_python selenium登录_Ustiniano的博客-CSDN博客 一、概述
我们要先安装selenium这个库使用pip install selenium 命令安装selenium这个库相当于机器模仿人的行为去点击浏览器上的元素这时我们要用到一个浏览器的驱动这里我用的是谷歌浏览器。 二、安装驱动 确认浏览器版本
首先我们先要查看自己浏览器的版本谷歌浏览器的话点右上角三个点--帮助--关于 Chrome
我们会看到自己的浏览器版本可以看到我的浏览器版本为100.0.4896.127正式版本
下载驱动
打开网页 CNPM Binaries Mirror
找到100.0.4896.127后面的小版本号虽然和我的浏览器有些差异可以忽略。只要保证大版本是一样即可。 点击进去找到windows版。注意windows版只有32位没有64位。 下载完后解压后里面有个chromedriver.exe文件
获取自己python安装的目录
打开cmd输入where python可以查看python安装的路径一般是下面这个如果找不到目录记得打开计算机文件隐藏项目 将解压后的chromedriver.exe文件复制到python安装目录下 三、 分析网页
打开某宝官网点击登录按f12查看网页源码定位到账号输入框、密码输入框和登录按钮复制它们的xpath 。 返回官网首页同样的方法复制搜索框和搜索按钮的xpath这里比如我输入电脑 接下来分析网页获取商品信息 这里我就放在代码里面了。 四、代码
代码这里我使用了一个滑块验证的方法滑块验证不一定会成功也可以自己手动滑一下。 import timeimport csvfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver import ChromeOptions, ActionChains# 定义爬取单页的函数def get_page(web):divs web.find_elements_by_xpath(//*[idmainsrp-itemlist]/div/div/div[1]/div)# print(divs)for div in divs:info div.find_element_by_xpath(./div[2]/div[2]/a).text # 商品名称price div.find_element_by_xpath(./div[2]/div[1]/div[1]/strong).text 元 # 商品价格deal div.find_element_by_xpath(./div[2]/div[1]/div[2]).text # 商品付款人数name div.find_element_by_xpath(./div[2]/div[3]/div[1]/a/span[2]).text # 商家店名print(info, price, deal, name, sep|)try:csvwriter.writerow([info, price, deal, name])except :passoption ChromeOptions()# 设置为开发者模式防止被各大网站识别出来使用了Seleniumoption.add_experimental_option(excludeSwitches, [enable-automation])option.add_argument(--disable-blink-features)option.add_argument(--disable-blink-featuresAutomationControlled)# 初始化一个web对象web webdriver.Chrome(optionsoption)# 进入淘宝官网web.get(https://www.taobao.com/)# 点击登录web.find_element_by_xpath(//*[idJ_SiteNavLogin]/div[1]/div[1]/a[1]).click()# 输入账号密码web.find_element_by_xpath(//*[idfm-login-id]).send_keys(你的手机号)web.find_element_by_xpath(//*[idfm-login-password]).send_keys(你的密码)# 点击登录web.find_element_by_xpath(//*[idlogin-form]/div[4]/button).click()time.sleep(2)# 搜索商品并回车web.find_element_by_xpath(//*[idq]).send_keys(电脑, Keys.ENTER)time.sleep(3)# 验证淘宝滑块在前三秒也可以手动滑块因为不确保自动滑块能成功try:yz web.find_element_by_xpath(//*[idbaxia-punish]/div[2]/div/div[1]/div[2]/div/p).textif yz 通过验证以确保正常访问:while 1:# 获取滑块的大小span_background web.find_element_by_xpath(//*[idnc_1__scale_text]/span)span_background_size span_background.size# print(span_background_size)# 获取滑块的位置button web.find_element_by_xpath(//*[idnc_1_n1z])button_location button.location# print(button_location)# 拖动操作drag_and_drop_by_offset# 将滑块的位置由初始位置右移一个滑动条长度即为x坐标在滑块位置基础上加上滑动条的长度y坐标保持滑块的坐标位置x_location span_background_size[width]y_location button_location[y]# print(x_location, y_location)action ActionChains(web)source web.find_element_by_xpath(//*[idnc_1_n1z])action.click_and_hold(source).perform()action.move_by_offset(x_location, 0)action.release().perform()time.sleep(1)try:web.find_element_by_xpath(//*[idnc_1_refresh1]).click()time.sleep(3)except:passexcept:with open(taobao.csv, modea, newline, encodinggbk) as fp:csvwriter csv.writer(fp, delimiter,)csvwriter.writerow([info, price, deal, name])Allpage 3count 0while count Allpage:count 1print(-------------------正在爬取第%d页--------------------- % count)get_page(web)web.find_element_by_xpath(//*[idmainsrp-pager]/div/div/div/ul/li[8]/a/span[1]).click()print(------------------------)time.sleep(5)web.close()web.quit()
最好不要用自己的账号过多的爬取可能会封号。