21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)
6.1.爬取第一页的职位信息
第一页职位信息
from selenium import webdriver
from lxml import etree
import re
import time class LagouSpider(object):
def __init__(self):
self.driver = webdriver.Chrome()
#python职位
self.url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
self.position = [] def run(self):
self.driver.get(self.url)
source = self.driver.page_source
self.parse_list_page(source) def parse_list_page(self,source):
html = etree.HTML(source)
links = html.xpath("//a[@class='position_link']/@href")
#每一页的所有职位的详情url
for link in links:
self.request_detail_page(link)
time.sleep(1) def request_detail_page(self,url):
self.driver.get(url)
#获取职位详情页的源代码
source = self.driver.page_source
self.parse_detail_page(source) def parse_detail_page(self,source):
html = etree.HTML(source)
position_name = html.xpath("//span[@class='name']/text()")[0]
job_request_spans = html.xpath("//dd[@class='job_request']//span")
salary = job_request_spans[0].xpath('.//text()')[0].strip()
city = job_request_spans[1].xpath('.//text()')[0].strip()
city = re.sub(r"[\s/]","",city)
work_years = job_request_spans[2].xpath('.//text()')[0].strip()
work_years = re.sub(r"[\s/]","",work_years)
education = job_request_spans[3].xpath('.//text()')[0].strip()
education = re.sub(r"[\s/]","",education)
desc = "".join(html.xpath("//dd[@class='job_bt']//text()")).strip()
position = {
'name':position_name,
'salary':salary,
'city': city,
'work_years': work_years,
'education': education,
'desc': desc,
}
self.position.append(position)
print(position)
print('-'*200) if __name__ == '__main__':
spider = LagouSpider()
spider.run()
6.2.爬取所有页的职位信息
from selenium import webdriver
from lxml import etree
import re
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By class LagouSpider(object):
def __init__(self):
self.driver = webdriver.Chrome()
#python职位
self.url = 'https://www.lagou.com/jobs/list_python?city=%E5%8C%97%E4%BA%AC&cl=false&fromSearch=true&labelWords=&suginput='
self.position = [] def run(self):
self.driver.get(self.url)
while True:
source = self.driver.page_source
WebDriverWait(driver=self.driver,timeout=20).until(
EC.presence_of_element_located((By.XPATH,"//div[@class='pager_container']/span[last()]"))
)
self.parse_list_page(source)
#点“下一页”
next_btn = self.driver.find_element_by_xpath(
"//div[@class='pager_container']/span[last()]")
if "pager_next_disabled" in next_btn.get_attribute("class"):
break
else:
next_btn.click()
time.sleep(1) def parse_list_page(self,source):
html = etree.HTML(source)
links = html.xpath("//a[@class='position_link']/@href")
#每一页的所有职位的详情url
for link in links:
self.request_detail_page(link)
time.sleep(1) def request_detail_page(self,url):
# self.driver.get(url)
self.driver.execute_script("window.open('%s')"%url)
self.driver.switch_to.window(self.driver.window_handles[1]) WebDriverWait(driver=self.driver,timeout=20).until(
EC.presence_of_element_located((By.XPATH,"//div[@class='job-name']/span[@class='name']"))
)
#获取职位详情页的源代码
source = self.driver.page_source
self.parse_detail_page(source)
#关闭当前详情页,并且切换到列表页
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[0]) def parse_detail_page(self,source):
html = etree.HTML(source)
position_name = html.xpath("//span[@class='name']/text()")[0]
job_request_spans = html.xpath("//dd[@class='job_request']//span")
salary = job_request_spans[0].xpath('.//text()')[0].strip()
city = job_request_spans[1].xpath('.//text()')[0].strip()
city = re.sub(r"[\s/]","",city)
work_years = job_request_spans[2].xpath('.//text()')[0].strip()
work_years = re.sub(r"[\s/]","",work_years)
education = job_request_spans[3].xpath('.//text()')[0].strip()
education = re.sub(r"[\s/]","",education)
desc = "".join(html.xpath("//dd[@class='job_bt']//text()")).strip()
company_name = html.xpath("//h2[@class='fl']/text()")[0].strip()
position = {
'name':position_name,
'company_name':company_name,
'salary':salary,
'city': city,
'work_years': work_years,
'education': education,
'desc': desc,
}
self.position.append(position)
print(position)
print('-'*200) if __name__ == '__main__':
spider = LagouSpider()
spider.run()
21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)的更多相关文章
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- 通俗易懂的分析如何用Python实现一只小爬虫,爬取拉勾网的职位信息
源代码:https://github.com/nnngu/LagouSpider 效果预览 思路 1.首先我们打开拉勾网,并搜索"java",显示出来的职位信息就是我们的目标. 2 ...
- ruby 爬虫爬取拉钩网职位信息,产生词云报告
思路:1.获取拉勾网搜索到职位的页数 2.调用接口获取职位id 3.根据职位id访问页面,匹配出关键字 url访问采用unirest,由于拉钩反爬虫,短时间内频繁访问会被限制访问,所以没有采用多线程, ...
- python3爬虫-通过requests获取拉钩职位信息
import requests, json, time, tablib def send_ajax_request(data: dict): try: ajax_response = session. ...
- selelinum+PhantomJS 爬取拉钩网职位
使用selenium+PhantomJS爬取拉钩网职位信息,保存在csv文件至本地磁盘 拉钩网的职位页面,点击下一页,职位信息加载,但是浏览器的url的不变,说明数据不是发送get请求得到的. 我们不 ...
- 利用Selenium爬取淘宝商品信息
一. Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...
- python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...
- Python爬虫项目--爬取自如网房源信息
本次爬取自如网房源信息所用到的知识点: 1. requests get请求 2. lxml解析html 3. Xpath 4. MongoDB存储 正文 1.分析目标站点 1. url: http:/ ...
- 爬虫—Selenium爬取JD商品信息
一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...
随机推荐
- Linux yum源配置
Linux yum源配置 本文介绍Red Hat下yum源配置方法,Redhat使用yum网络源需要购买服务,但是本地yum源不会收费. CentOS用户自带yum源,并且yum不收费. 准备工具: ...
- WM-结汇
PI发给客户以后, 客户打过来定金之后. 把PI的 P单词去掉,改为只剩invoice, 再把PI改成一份contract为标题的文件,两份文件都是要自己和客户的签字的. 然后需要用这两份文件进行结汇 ...
- POJ-2533.Longest Ordered Subsequence (LIS模版题)
本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...
- Selenium 学习汇总
Commands (命令) Action 对当前状态进行操作 失败时,停止测试 Assertion 校验是否有产生正确的值 Element Locators 指定HTML中的某元素 Patterns ...
- spring入门——applicationContext与BeanFactory的区别
我们知道从applicationContext容器对象中如何获取Bean了,其实spring框架还有另外一种获取bean的方法:BeanFactory代码如下: BeanFactory factory ...
- Spring Boot 整合mybatis 使用多数据源
本人想要实现一个项目里面多个数据库源连接,所以就尝试写一个demo,不多说,先贴结构,再贴代码,可以根据以下的顺序,直接copy解决问题. 首先,dao和resource下的mappers可以用myb ...
- 真·浅谈treap树
treap树是一种平衡树,它有平衡树的性质,满足堆的性质,是二叉搜索树,但是我们需要维护他 为什么满足堆的性质?因为每个节点还有一个随机权值,按照随机权值维持这个堆(树),可以用O(logn)的复杂度 ...
- boost asio 学习(三)post与dispatch
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4 本章节为io_ ...
- VBA找相似体积的单元格值
在VBA中做了一个比较体积,如果体积相似就显示隔壁单元格的内容 Function VC(a, b As Range) 'VolumeCompare体积比较 Dim arry() As Variant ...
- C语言编程常见技巧(问题???)
本文章根据<算法竞赛入门经典(第二版)>一书整理... 第一章 程序设计入门 printf 语句控制输出小数位数或总长度 printf("%.3f\n",8.0/5.0 ...