当前位置:   article > 正文

python-selenium爬虫操作_selenium爬虫的工作流程

selenium爬虫的工作流程

本文是基于python的selenium爬虫操作


前言

Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
这个地方百度百科过来的,主要是让模拟人的操作,或者自动化之类的操作


一、selenium爬虫

selenium爬虫是爬虫板块一个比较重要的部分,他可以解决很多问题,比如:直接解析网页解析不了,或者动态加载的网页,还有需要点击之类的操作,selenium是一个很好的解决办法。


这里值得一提的是:js加载或者ajax加载,emm,可以考虑抓包的方式。

推荐抓包软件:Fiddler

抓包的话后面有空再说这里先说selenium
加一个selenium官方文档链接:selenium官方文档.
emm,看不懂的可以选择中午文档了解
在这里插入图片描述

二、使用步骤

1.安装

说明一下安装

pip install selenium
  • 1

这玩意结束后
下载一个chromedriver插件,这里我只说谷歌版的,我用的谷歌。
下载链接好了插件下载链接.
这有个小细节,就是你要先确认你的谷歌版本号然后再去选择你要的,一般x64就下载64位,但是64也会兼容32,一般是向下兼容。
在这里插入图片描述

方法一:
下载好了就把这个放在有环境配置的地方,就可以了
方法二:
这个办法不行的话
可以换一个:
直接调用:

from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = 'D:\Program Files\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(chrome_options=option)
  • 1
  • 2
  • 3
  • 4

这个是自己定位chrome的位置去调用。

2.爬虫代码部分

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import pandas as pd
import re
from lxml import etree
import time

option = webdriver.ChromeOptions()
option.binary_location = 'D:\Program Files\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(chrome_options=option)
driver.get(
    'https://search.51job.com/list/060000,000000,0000,00,9,99,%2B,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=')
wait = WebDriverWait(driver, 10)

search_btn = driver.find_element_by_css_selector(
    '#keywordInput'
)
input1 = search_btn.send_keys('数据分析')

# 点击搜索
confirm_btn = wait.until(
    EC.element_to_be_clickable(
        (By.CSS_SELECTOR, '#search_btn')
    )
)
data1 = confirm_btn.click()
#点击连接
#对点击连接作循环
a=1
while True:
    next_to_page = wait.until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > div:nth-child(4) > div.j_result > div > div.leftbox > div:nth-child(4) > div.j_page > div > div > div > ul > li.next > a')))

    html = etree.HTML(driver.page_source)
    rqq = html.xpath('//div[@class="j_joblist"]/div[@class="e"]')

    job3=[]
    for p in rqq:
        item2={}
        link = p.xpath('./a/@href')[0]
        driver.get(link)
        html2 = etree.HTML(driver.page_source)
        info = html2.xpath('/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]/text()')
        info1 = re.sub('\s',"",info[0]).split('|')
        item2['条件'] = ",".join(info1[1:-2])
        num = re.findall("(\d+)人",info[-2])[0] if len(re.findall("(\d+)人",info[-2]))>0 else "若干"
        item2['招聘人员'] = num
        treatment = html2.xpath('/html/body/div[3]/div[2]/div[2]/div/div[1]/div/div/span/text()')
        item2['待遇'] = treatment
        job3.append(item2)
        time.sleep(3)
    if a > 1:
        break
    else:
        a = a + 1
    next_to_page.click()

data3=pd.DataFrame(job3)
print(data3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

运行结果:
在这里插入图片描述


三、简单有用的操作

这里讲一些selenium的简单操作:

动态加载只显示部分的网页需要滑动网页到最下面的:

driver.execute_script("window.scrollTo(0,1680)")
    # 直接拖动到底部
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

  • 1
  • 2
  • 3
  • 4

比如爬取京东selenium可以将网页拉到最下面进行点击


 windows = driver.window_handles
# 切换到当前最新打开的窗口
driver.switch_to.window(windows[-1])
  • 1
  • 2
  • 3

切换到新打开的页面。


网页的向前向后
driver.back() #向后
driver.forward() #向前
driver.close() #关闭当前页面
driver.quit() #直接关闭你打开的网页,整个
driver.page_source()  #这个获取网页源码
  • 1
  • 2
  • 3
  • 4
  • 5

这个地方还得提一点:
网页等待,这玩意分为显式和隐式以及强制等待:
①显式等待:
这个地方可以这么理解:直到什么才做什么,同时也在不断的检测,返回,确定需要的地方是否已经加载完毕
上面的爬虫代码中就运用了很多的显式等待

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import pandas as pd
import re
from lxml import etree
import time

option = webdriver.ChromeOptions()
option.binary_location = 'D:\Program Files\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(chrome_options=option)
driver.get(
    'https://search.51job.com/list/060000,000000,0000,00,9,99,%2B,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=')
wait = WebDriverWait(driver, 10)
# 点击搜索
confirm_btn = wait.until(
    EC.element_to_be_clickable(
        (By.CSS_SELECTOR, '#search_btn')
    )
)
data1 = confirm_btn.click()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

②隐式等待:
一般这个是等到网页加载完毕后,在进行下一步,否则会等到时间结束才进行下一步,但是这个缺点也是比较明显的,容易造成加载超时。

driver.implicitly_wait(15)
一般用于加载网页的等待
  • 1
  • 2

③强制等待:强制性等待时间结束:然后才运行接下来的代码部分
一般是time模块下的

import time
import random
time.sleep(3)
或者考虑随机随眠时间
time.sleep(random.randint(1,5))
随机取1到5之内的数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

emm,到这里应该也差不多了,后面还有什么东西,我再来补充
干饭干饭!!!
在这里插入图片描述


注:本文中如有错误部分,劳烦指正,谢谢啦。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/839596?site
推荐阅读
相关标签
  

闽ICP备14008679号