二.selenium爬取51job任意职位信息

#coding:utf-8
import HTMLParser
from selenium import webdriver
import time,os,requests,xlwt,xlrd
from lxml import etree
from xlutils.copy import copy
from bs4 import  BeautifulSoup

def url_do(path,job):

    headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0'}
    url='http://search.51job.com/jobsearch/advance_search.php'
    driver=webdriver.PhantomJS(executable_path="E:\\mac\\id\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
    try:
        driver.set_page_load_timeout(10)
        driver.get(url)
        driver.find_element_by_id('kwdselectid').send_keys(job)
        driver.find_element_by_css_selector('.p_but').click()
    except Exception as e:
        print e
        driver.quit()
    else:
        next_page=driver.find_element_by_link_text(u'下一页')
        print driver.current_url
        while next_page:
            new_url=driver.current_url
            text=requests.get(new_url,headers=headers)
            et=etree.HTML(text.content)
            line=et.xpath("//div[@class='el']")
            for i in line:
                tx=etree.tostring(i,pretty_print=True)
                cont=HTMLParser.HTMLParser().unescape(tx)
                readtext(cont,path)
            driver.find_element_by_link_text(u'下一页').click()
            print driver.current_url
        driver.quit()
def readtext(cont,path):
    print cont
    a=b=c=d=e=f=''
    a=txt_wrap_by('html">','</',cont)
    b=txt_wrap_by('href="','" onmousedown',cont)
    c=txt_wrap_by('">','</',cont)
    d=txt_wrap_by('">','</',cont)
    e=txt_wrap_by('">','</',cont)
    f=txt_wrap_by('title="','" href',cont)
    list_data=[f,b,a,c,d,e]
    for i in list_data:
        print i
    write_xls(list_data,path)


def write_xls(list_data,path):
    wk=xlrd.open_workbook(path)
    sheet=wk.sheet_by_name('Sheet1')
    wb=copy(wk)
    for i in list_data:
        wb.get_sheet(0).write(sheet.nrows,list_data.index(i),i)
    wb.save(path)

def txt_wrap_by(start_str, end, html):
    start = html.find(start_str)
    if start >= 0:
        start += len(start_str)
        end = html.find(end, start)
        if end >= 0:
            return html[start:end].strip()
def  testexist(path):
    if not os.path.exists(path):
        w= xlwt.Workbook()
        w.add_sheet('Sheet1')
        w.save(path)
    else:
        os.remove(path)
        w= xlwt.Workbook()
        w.add_sheet('Sheet1')
        w.save(path)
if __name__=='__main__':
    path='E:\\0930\\ceshi.xls'
    testexist(path)
    #随意输入职位
    job=u'运维工程师'
    url_do(path,job)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是关于使用Python和Selenium爬取51job的简要步骤: 1. 安装Python和Selenium 2. 下载Chrome浏览器和对应版本的ChromeDriver 3. 编写Python脚本,使用Selenium打开Chrome浏览器,访问51job网站,并输入关键词进行搜索 4. 使用Selenium模拟鼠标滚动,加载更多的职位信息 5. 使用BeautifulSoup或正则表达式等方法解析网页内容,提取所需信息 6. 将提取的信息保存到本地文件或数据库中 需要注意的是,爬取网站时需要遵守相关法律法规和网站的使用协议,不得进行恶意爬取和侵犯他人隐私等行为。 ### 回答2: Python Selenium是一个用于自动化测试的Python库。它可以模拟用户在网页上的操作并提取网页的信息。本篇文章将介绍如何使用Python Selenium爬取51job的招聘信息。以下是具体步骤: 1. 安装Selenium和Chrome浏览器 在Windows平台下,安装Selenium的命令为:pip install selenium。接着,安装Chrome浏览器,并在本地环境中安装ChromeDriver,它是Selenium的浏览器驱动程序。下载地址为:https://sites.google.com/a/chromium.org/chromedriver/home 2. 连接到51job网站 通过Selenium启动Chrome浏览器并连接到51job的招聘页面。查找“关键字”、“地点”、“职能类别”和“发布时间”的输入框并依次输入搜索条件。通过点击“搜索”按钮,进入到搜索结果页面。 3. 爬取搜索结果 从搜索结果页面获取每个招聘信息的标题、公司名称、薪酬、发布日期和详细链接。将提取的信息存储在列表中。 4. 翻页爬取结果 对于大量的搜索结果,需要翻页操作来获取全部招聘信息。对于51job网站,通过点击“下一页”按钮实现翻页。循环爬取多页的招聘信息并存储在列表中。 5. 存储数据 将提取的招聘信息存储在CSV文件或其他格式中,方便后续的分析和处理。 6. 结束程序 关闭浏览器和ChromeDriver,程序结束运行。 综上所述,使用Python Selenium可以方便地爬取51job的招聘信息,并用于后续的数据分析和处理。 ### 回答3: Python Selenium是一种基于Python的自动化测试工具,它主要用于模拟用户在浏览器上的操作,以此实现网络爬虫的功能。51job是一个国内较为知名的招聘网站,如果我们想要批量获取招聘信息,可以使用Python Selenium进行爬取。 为了使用Python Selenium爬取51job,我们需要安装Selenium库以及相应的web driver(如Chrome driver)。 首先,我们需要进入51job的招聘页面,分析其中的HTML结构,确定需要爬取信息。通常招聘信息包括公司名称、职位、学历要求、薪资等信息。然后根据这些信息,编写Python脚本,使用Selenium模拟用户操作,依次打开网页、输入搜索条件、点击搜索按钮、获取每页的职位信息等。 具体实现步骤如下: 1. 首先,我们需要导入Selenium库和web driver。在本例中,我们使用Chrome driver作为web driver,并设置一些选项,以便快速打开和关闭浏览器。 import time from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--no-sandbox') browser = webdriver.Chrome(chrome_options=chrome_options) browser.implicitly_wait(10) 2. 打开51job的招聘页面。 browser.get('https://www.51job.com/') 3. 模拟输入搜索条件。在51job的搜索页面,我们可以通过定位输入框并使用send_keys()方法进行模拟输入。 input_box = browser.find_element_by_id('kwdselectid') input_box.send_keys('Python') 4. 模拟点击搜索按钮。同样,我们可以通过定位按钮并使用click()方法模拟点击事件。 search_button = browser.find_element_by_css_selector('div[class="fltr"][id="searchbtn"]') search_button.click() 5. 遍历每一页并获取职位信息。在51job中,职位信息被包含在一个类名为“dw_table”的表格中。我们可以使用find_elements_by_css_selector()方法获取这个表格,再使用for循环遍历其中的每一行,获取并打印出每行的职位信息。 table = browser.find_elements_by_css_selector('div[class="el"]')[1:] for row in table: company = row.find_element_by_css_selector('span[class="t2"]').text position = row.find_element_by_css_selector('a').text education = row.find_element_by_css_selector('span[class="t3"]').text salary = row.find_element_by_css_selector('span[class="t4"]').text print(company, position, education, salary) 最后,我们可以关闭浏览器。 browser.quit() 综上所述,Python Selenium是一种功能强大的网络爬虫工具,可以帮助我们快速爬取网页上的大量招聘信息。它可以模拟用户在浏览器上的操作,让我们可以轻松爬取需要的信息,并在数据分析和处理中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值