爬虫_拉勾网(解析ajax)
拉勾网反爬虫做的比较严,请求头多添加几个参数才能不被网站识别
找到真正的请求网址,返回的是一个json串,解析这个json串即可,而且注意是post传值
通过改变data中pn的值来控制翻页
job_name读取的结果是一个列表 ['JAVA高级工程师、爬虫工程师'] ,而我只想得到里面的字符串,在用job_name[0]的时候,爬取过程中会报下标错误,不知道怎么回事,都看了一遍没问题啊,只能不处理这个列表了
import requests
from lxml import etree
import time
import re headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36',
'Cookie': '_ga=GA1.2.438907938.1533109215; user_trace_token=20180801154015-21630e48-955e-11e8-ac93-525400f775ce; LGUID=20180801154015-216316d6-955e-11e8-ac93-525400f775ce; WEBTJ-ID=08122018%2C193417-1652dea595ec0a-0ca5deb5d1dcb9-172f1503-2073600-1652dea595f51f; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1533109214,1534073658; LGSID=20180812193418-a61634a5-9e23-11e8-bae9-525400f775ce; PRE_UTM=m_cf_cpt_baidu_pc; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D0%26rsv_idx%3D1%26tn%3Dbaidu%26wd%3Dlagou%2520wang%2520%26rsv_pq%3D8f8c8d4500066afd%26rsv_t%3Deaa21Vp5dXD8YMkjoybb0H4UW4n0ReZfxSBiBne14tXfDsN5XKFydx2jVnA%26rqlang%3Dcn%26rsv_enter%3D1%26rsv_sug3%3D12%26rsv_sug1%3D2%26rsv_sug7%3D101%26rsv_sug2%3D0%26inputT%3D1970%26rsv_sug4%3D3064; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Flp%2Fhtml%2Fcommon.html%3Futm_source%3Dm_cf_cpt_baidu_pc; _gid=GA1.2.849726101.1534073658; X_HTTP_TOKEN=0d3a98743216c83f14e092a5154d2327; LG_LOGIN_USER_ID=5f3d6b41f24b2423bae0a956753632482a2ecabcb36ef97074ff9fdec683ae51; _putrc=3A0548631F844F3C123F89F2B170EADC; JSESSIONID=ABAAABAAAIAACBI302EB6DF4BD54C909C01751F65D4311D; login=true; unick=%E5%88%98%E4%BA%9A%E6%96%8C; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=0; gate_login_token=c6678499c0aaeb807c1e37249f90d483317ec272762e7060fa18175f49d9c806; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=search_code; _gat=1; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1534074635; LGRID=20180812195035-ec87e3ce-9e25-11e8-a37b-5254005c3644; SEARCH_ID=85704a0c1aaf43d4b9f07489311f0707',
'Referer': 'https://www.lagou.com/jobs/list_%E7%88%AC%E8%99%AB?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=sug&suginput=pachong',
'Origin': 'https://www.lagou.com'
} data = {
'first': 'false',
'pn': 1,
'kd': '爬虫'
} def get_list_page(page_num):
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
data['pn'] = page_num
response = requests.post(url, headers=headers, data=data)
result = response.json()
positions = result['content']['positionResult']['result']
for position in positions:
positionId = position['positionId']
position_url = 'https://www.lagou.com/jobs/%s.html' %positionId parse_detail_page(position_url)
# break def parse_detail_page(url):
job_information = {}
response = requests.get(url, headers=headers)
# response.raise_for_status()
html = response.text
html_element = etree.HTML(html)
job_name = html_element.xpath('//div[@class="job-name"]/@title')
job_description = html_element.xpath('//dd[@class="job_bt"]//p//text()')
for index, i in enumerate(job_description):
job_description[index] = re.sub('\xa0', '', i)
job_address = html_element.xpath('//div[@class="work_addr"]/a/text()')
job_salary = html_element.xpath('//span[@class="salary"]/text()') # 字符串处理去掉不必要的信息
for index, i in enumerate(job_address):
job_address[index] = re.sub('查看地图', '', i)
while '' in job_address:
job_address.remove('') # job_address_detail = html_element.xpath('//div[@class="work_addr"]/a[-2]/text()')
# print(job_address_detail)
job_information['job_name'] = job_name
job_information['job_description'] = job_description
job_information['job_address'] = job_address
job_information['job_salary'] = job_salary
print(job_information) def main():
for i in range(1, 31):
get_list_page(i)
print('='*30 + '第%s页'%i + '='*30) if __name__ == '__main__':
main()
运行结果

爬虫_拉勾网(解析ajax)的更多相关文章
- 爬虫_拉勾网(selenium)
使用selenium进行翻页获取职位链接,再对链接进行解析 会爬取到部分空列表,感觉是网速太慢了,加了time.sleep()还是会有空列表 from selenium import webdrive ...
- Python网络爬虫_爬取Ajax动态加载和翻页时url不变的网页
1 . 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新 ...
- 笔记-爬虫-js代码解析
笔记-爬虫-js代码解析 1. js代码解析 1.1. 前言 在爬取网站时经常会有js生成关键信息,而且js代码是混淆过的. 以瓜子二手车为例,直接请求https://www.guaz ...
- jQuery解析AJAX返回的html数据时碰到的问题与解决
$.ajax({ type : "post", url : "<%=request.getContextPath()%>/ce/articledetail/m ...
- python爬虫---爬虫的数据解析的流程和解析数据的几种方式
python爬虫---爬虫的数据解析的流程和解析数据的几种方式 一丶爬虫数据解析 概念:将一整张页面中的局部数据进行提取/解析 作用:用来实现聚焦爬虫的吧 实现方式: 正则 (针对字符串) bs4 x ...
- 20170717_python_爬虫_网页数据解析_BeautifulSoup_数据保存_pymysql
上午废了老大劲成功登陆后,下午看了下BeautifulSoup和pymysql,晚上记录一下 自己电脑装的sublime,字体颜色竟然拷贝不下来 - - 写的过程中遇到了很多问题: 1.模拟登陆部分 ...
- python爬虫_简单使用百度OCR解析验证码
百度技术文档 首先要注册百度云账号: 在首页,找到图像识别,创建应用,选择相应的功能,创建 安装接口模块: pip install baidu-aip 简单识别一: 简单图形验证码: 图片: from ...
- 【Python3爬虫】拉勾网爬虫
一.思路分析: 在之前写拉勾网的爬虫的时候,总是得到下面这个结果(真是头疼),当你看到下面这个结果的时候,也就意味着被反爬了,因为一些网站会有相应的反爬虫措施,例如很多网站会检测某一段时间某个IP的访 ...
- PYTHON 爬虫笔记九:利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集(实战项目二)
利用Ajax+正则表达式+BeautifulSoup爬取今日头条街拍图集 目标站点分析 今日头条这类的网站制作,从数据形式,CSS样式都是通过数据接口的样式来决定的,所以它的抓取方法和其他网页的抓取方 ...
随机推荐
- An error occurred while updating the entries. See the inner exception for details.
EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...
- Nice Garland CodeForces - 1108C (思维+暴力)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- UIAutomatorViewer 出现错误:Unable to connect to adb
最近升级了AndroidSDK,打开UIAutomatorViewer.bat,结果发现获取不了Android设备界面上的UI信息.经过一番努力,终于把这个问题解决了,详细过程如下: 1. Unabl ...
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- C#复习笔记(5)--C#5:简化的异步编程(异步编程的深入分析)
首先,阐明一下标题的这个“深入分析”起得很惭愧,但是又不知道该起什么名字,这个系列也主要是做一些复习的笔记,供自己以后查阅,如果能够帮助到别人,那自然是再好不过了. 然后,我想说的是异步方法的状态机真 ...
- [转帖]TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大
TLS 1.3 VS TLS 1.2,让你明白 TLS 1.3 的强大 https://www.jianshu.com/p/efe44d4a7501?utm_source=oschina-app 又拍 ...
- day 7-11 初识MySQL数据库及安装密码设置破解
一. 什么是数据库 之前所学,数据要永久保存,比如用户注册的用户信息,都是保存于文件中,而文件只能存在于某一台机器上. 如果我们不考虑从文件中读取数据的效率问题,并且假设我们的程序所有的组件都运行在一 ...
- java中级——集合框架【4】-Collections
Collections 首先我们要知道Collections是一个类,容器的工具类,就如同Arrays是数组的工具类 反转 reverse 使List中的数据发生发转 package cn.jse.c ...
- Android——Activity的简绍
Activity Activity的运行机制其实和JavaEE中的servlet很像,而我们的Android系统也就相当与其servlet容器,Activity在其中进行创建实例.初始化.运行.销毁等 ...
- Java语言中姐种遍历List的方法总结
遍历 List 的方法: 1. for 2. advanced for 3. Iterator 4. while 5. ListIterator List<E> list 1. for f ...