和昨天一样的工作量,时间只用了一半,但还是效率有点低了,因为要把两个网页结合起来,所以在列表操作上用了好多时间

 import requests
from lxml import etree headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'} def get_html(url):
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
html = response.text
return html def parse_html(html):
informations = []
urls = []
html_element = etree.HTML(html)
kinds = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])/td[2]/text()')
'''
kinds:
['技术类', '设计类', '技术类', '技术类', '技术类', '技术类', '技术类', '技术类', '技术类', '产品/项目类']
'''
nums = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])//td[3]/text()')
'''
nums:
['2', '1', '2', '1', '2', '2', '1', '2', '1', '1']
'''
addresses = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])//td[4]/text()')
'''
addresses:
['深圳', '深圳', '深圳', '深圳', '深圳', '深圳', '深圳', '深圳', '深圳', '深圳']
'''
times = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])//td[5]/text()')
'''
times:
['2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04', '2018-08-04']
'''
names = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])//a/text()') detail_url = html_element.xpath('(//tr[@class="even"]|//tr[@class="odd"])//a/@href')
for str_url in detail_url: url = 'https://hr.tencent.com/' + str(str_url)
urls.append(url) '''
urls :
['https://hr.tencent.com/position_detail.php?id=42917&keywords=python&tid=0&lid=0',
'https://hr.tencent.com/position_detail.php?id=42908&keywords=python&tid=0&lid=0',
......
'https://hr.tencent.com/position_detail.php?id=42832&keywords=python&tid=0&lid=0',
'https://hr.tencent.com/position_detail.php?id=42628&keywords=python&tid=0&lid=0']
'''
for index, name in enumerate(names):
information = {}
information['name'] = name
information['url'] = urls[index]
information['kind'] = kinds[index]
information['nums_of_need'] = nums[index]
information['address'] = addresses[index]
informations.append(information)
# print(informations)
# print(urls)
return urls, informations def parse_detail_page(url):
#one detail page
html = get_html(url)
return html def get_all_page(page_nums):
for i in range(0, page_nums):
url = 'https://hr.tencent.com/position.php?lid=&tid=&keywords=python&start={0}#a'.format(i*10)
html = get_html(url)
urls, informations = parse_html(html)
# print(informations)
works = []
for i, url in enumerate(urls): html_detail = parse_detail_page(url)
html_element = etree.HTML(html_detail)
work_intro = html_element.xpath('//td[@class="l2"]//text()')
for index, text in enumerate(work_intro):
if text.startswith('工作职责:'):
text = text.replace('工作职责:', '')
works_detail = {}
intros = []
for x in range(index+1, len(work_intro)):
intro = work_intro[x].strip()
if work_intro[x].startswith('工作要求:'):
break
intros.append(intro)
while '' in intros:
intros.remove('')
works_detail['1_____工作职责:'] = intros
works.append(works_detail)
# print(intros)
'''
['负责NLP与深度学习相关技术的研究与实现;',
'负责建设基础的语义分析工具和平台;',
'负责搜索系统、知识图谱系统、问答与对话系统的设计与搭建;',
'结合实际业务需求与数据,研发高效、稳健、完备的NLP解决方案。']
''' if text.startswith('工作要求:'):
text = text.replace('工作要求:', '')
works_detail = {}
requests = []
for x in range(index+1, len(work_intro)):
intro = work_intro[x].strip()
if work_intro[x].startswith('申请岗位'):
break
requests.append(intro)
while '' in requests:
requests.remove('')
works_detail['2_____工作要求:'] = requests
works.append(works_detail)
# print(requests)
'''
['三年以上自然语言处理经验包括语义表示、搜索、知识图谱、对话系统等;',
'扎实的编程基础,至少精通一种编程语言,如C++,Java,python等;',
'熟悉深度学习以及常见机器学习算法的原理与算法,能熟练运用聚类、分类、回归、排序等模型解决有挑战性的问题;',
'对自然语言处理相关的分词、词性标注、实体识别、句法分析、语义分析等有深入的实践经验;',
'有强烈求知欲,对人工智能领域相关技术有热情;', '具有良好的数学基础,良好的英语阅读能力;',
'有项目管理经验,与他人合作良好,能够独立有效推动复杂项目。']
'''
return works, informations def main():
works, informations = get_all_page(1)
for index, information in enumerate(informations):
list = []
list.append(works[index*2])
list.append(works[index*2+1])
information['duty'] = list
print(information) if __name__ == '__main__':
main()

目前sublime还输入不了中文,所以把输出注释上,方便看清格式

运行结果:

红色圈出来的是一个字典,包含第一个网页的信息(职位名称,url,位置)和详情页面的职责(工作职责,工作要求),嵌套的可能有点复杂,但目前还没有想到更简明的方法

爬虫_腾讯招聘(xpath)的更多相关文章

  1. python爬虫入门(三)XPATH和BeautifulSoup4

    XML和XPATH 用正则处理HTML文档很麻烦,我们可以先将 HTML文件 转换成 XML文档,然后用 XPath 查找 HTML 节点或元素. XML 指可扩展标记语言(EXtensible Ma ...

  2. Scrapy实现腾讯招聘网信息爬取【Python】

    一.腾讯招聘网 二.代码实现 1.spider爬虫 # -*- coding: utf-8 -*- import scrapy from Tencent.items import TencentIte ...

  3. pymongodb的使用和一个腾讯招聘爬取的案例

    一.在python3中操作mongodb 1.连接条件 安装好pymongo库 启动mongodb的服务端(如果是前台启动后就不关闭窗口,窗口关闭后服务端也会跟着关闭) 3.使用 import pym ...

  4. 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息

    简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...

  5. 学习笔记之Python全栈开发/人工智能公开课_腾讯课堂

    Python全栈开发/人工智能公开课_腾讯课堂 https://ke.qq.com/course/190378 https://github.com/haoran119/ke.qq.com.pytho ...

  6. Scrapy 项目:腾讯招聘

    目的: 通过爬取腾讯招聘网站(https://careers.tencent.com/search.html)练习Scrapy框架的使用 步骤: 1.通过抓包确认要抓取的内容是否在当前url地址中,测 ...

  7. 历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网

    历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网 历峰集团3.43亿美元收购Net-a-Porter剩余股权

  8. 大Q品牌故事_大Q官网_腾讯旗下买卖宝公司倾力打造

    大Q品牌故事_大Q官网_腾讯旗下买卖宝公司倾力打造 走在大路上的改变者,有态度的互联网手机品牌

  9. 凡客副总裁崔晓琦离职 曾负责旗下V+商城项目_科技_腾讯网

    凡客副总裁崔晓琦离职 曾负责旗下V+商城项目_科技_腾讯网 凡客副总裁崔晓琦离职 曾负责旗下V+商城项目 腾讯科技[微博]乐天2013年09月18日12:44 分享 微博 空间 微信 新浪微博 邮箱 ...

随机推荐

  1. Less or Equal CodeForces - 977C (sort+细节)

    You are given a sequence of integers of length nn and integer number kk. You should print any intege ...

  2. Python_计算文件夹大小

    计算文件夹大小 os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.path.join(path1[, path2[, ...]]) 将 ...

  3. Python_迭代器、生成器、列表推导式,生成器表达式

    1.迭代器 (1)可迭代对象 s1 = ' for i in s1: print(i) 可迭代对象 示例结果: D:\Python36\python.exe "E:/Python/课堂视频/ ...

  4. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bw.mapper.BillMapper.getBillList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225

    这个错误是没有找到映射文件 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.b ...

  5. Ubuntu端口开放

    一.关于iptable的介绍 维基百科:https://zh.wikipedia.org/wiki/Iptables 注意:iptables的操作需要root权限 二.具体操作 sudo apt-ge ...

  6. HowTos/Virtualization/VirtualBox - CentOS Wiki

    https://wiki.centos.org/HowTos/Virtualization/VirtualBox

  7. PHP 高并发秒杀解决方案

    本文提供 PHP 高并发秒杀解决方案(附加三个案例说明(普通流程,使用文件锁,使用redis消息队列)) 1:(正常流程,不做任何高并发处理),代码如下: <?php $_mysqli = ne ...

  8. java.lang(StringBuffer)

    public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS ...

  9. jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等

    转载:https://blog.csdn.net/chenchunlin526/article/details/77448168 jQuery操作复选框checkbox技巧总结 --- 设置选中.取消 ...

  10. 运行Spark-shell,解决Unable to load native-hadoop library for your platform

    启动spark后,运行bin/spark-shell会出现一个警告 提君博客原创 WARN util.NativeCodeLoader: Unable to load native-hadoop li ...