爬完第一页就报错

来源:6-2 实战—selenium实现51job全站点岗位信息自动化抓取-2

allenyao

2019-08-11 17:27:08

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from lxml import etree
import time


class WebDriver(object):
    def __init__(self):
    super(WebDriver, self).__init__()
    chrome_opt = Options()
    # 设置为无头
    chrome_opt.add_argument('--headless')
    self.driver = webdriver.Chrome(chrome_options=chrome_opt)
    self.driver.maximize_window()

    def handle_job(self):
        self.driver.get("https://search.51job.com/list/000000,000000,0000,00,9,99,%2520,2,1.html"
)
        if WebDriverWait(self.driver, 5, poll_frequency=0.5).until(EC.presence_of_element_located((By.ID, "kwdselectid"))):
            input_kw = input("\t请输入要查找的岗位信息:")
            self.driver.find_element_by_id("kwdselectid").send_keys(input_kw)
            self.driver.find_element_by_class_name("p_but").click()
            if WebDriverWait(self.driver, 5, poll_frequency=0.5).until(EC.presence_of_element_located((By.ID, "resultList"))):
                while True:
                    self.parse_html(self.driver.page_source)
                    if self.driver.find_element_by_xpath("//li[@class='bk'][2]/a").text == "下一页":
                        self.driver.find_element_by_xpath("//li[@class='bk'][2]/a").click()
                    else:
                        break
            self.driver.quit()
            # time.sleep(5)
            # self.driver.quit()

    def parse_html(self, page_source):
        data = etree.HTML(page_source)
        all_div = data.xpath("//div[@id='resultList']//div[@class='el']")
        info_list = []
        for item in all_div:
            info = {}
            # . 代表的是使用item下的Xpath语句
            # 获取数据的时候,要使用列表索引为0的数据
            # 插入工作名称数据
            info['jobname'] = item.xpath("./p/span/a/@title")[0]
            # 插入公司名称数据
            info['company_name'] = item.xpath(".//span[@class='t2']/a/@title")[0]
            # 插入公司地区数据
            info['company_address'] = item.xpath(".//span[@class='t3']/text()")[0]
            # 此字段数据有为空的情况,所以需要现判断下抓过来的是不是为空
            temp = item.xpath(".//span[@class='t4']/text()")
            if len(temp) == 0:
            # 为空则插入“无”
                info['money'] = "无"
            else:
            # 不为空则插入爬回来的数据
                info['money'] = item.xpath(".//span[@class='t4']/text()")[0]
            # 插入发布日期数据
            info['date'] = item.xpath(".//span[@class='t5']/text()")[0]
            print(info)
        info_list.append(info)
        # return info_list


test = WebDriver()
test.handle_job()

异常如下

http://img.mukewang.com/climg/5d4fdf570001f82326180560.jpg

写回答

3回答

JiaXionG_Lynn

2019-10-04

还有就是老师这段代码是有点问题的

http://img.mukewang.com/climg/5d96f04a0904dbb706970100.jpg

这段代码如果正的抓取到最后一页的时候

self.driver.find_element_by_xpath("//li[@class='bk'][2]/a")

这条指令会报错的,因为最后一页用xpath没找到这个标签,python会报错,而不会执行if语句判断它是否为'下一页'

把这段断码改一下就可以了

while True:
    self.parse_html(self.drive.page_source)
    try:
        result = self.drive.find_element_by_xpath('//li[@class="bk"][2]/a').text
    except:
        result = False
    if result == '下一页':
        self.drive.find_element_by_xpath('//li[@class="bk"][2]/a').click()
    else:
        break


这是我自己的理解,同学可以参考一下

0

JiaXionG_Lynn

2019-10-04

http://img.mukewang.com/climg/5d96edec09bd85e304930106.jpg

把chrome_opt改为self.chrome_opt就可以解决了

def __init(self):
    self.chrome_opt = Options()
    self.chrome_opt.add_argument('--headless')
    self.drive = webdriver.Chrome(chrome_options=self.chrome_opt)
    self.drive.maximize_window()


0

allenyao

提问者

2019-08-11

去掉无头设置之后,就一切正常了。

0

0 学习 · 1672 问题

查看课程