由于拉勾网的cookie信息很快会过期,所以在这里我们可以利用selenium来模拟浏览器的行为,避开cookie信息对我们的限制。在上一篇,我们提取拉勾网的信息要自己手动地设置一些地址,这就很不方便了。
在这里我们用selenium打开拉勾网的首页,关闭弹窗,在输入框输入信息并点击搜索,像人一样点开每一个职位的详情页面提取完信息后关闭页面并打开下一个信息,完成一个页面后点击下一页并判断是否在最后一页,提取完信息后退出。完整代码如下
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from lxml import etree
import time
import re
class LagouSpider(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.positions = []
def run(self):
self.parse_main_page()
while True:
self.parse_list_page()
# 获取下一页点击按钮
next_page = self.driver.find_element_by_xpath("//div[@class='pager_container']/span[last()]")
# 判断是否到达最后一页

本文介绍了如何利用selenium模拟浏览器行为,避开cookie限制,从拉勾网抓取信息。通过打开首页、关闭弹窗、输入搜索关键词、遍历并提取每个职位详情页信息,实现自动化爬虫。代码详细展示了这一过程。
最低0.47元/天 解锁文章
772

被折叠的 条评论
为什么被折叠?



