作为爬虫的基础知识,我们都知道网络爬虫的数据主要来自服务器的响应结果,通常有html和json数据等,但是这两种数据是主要的数据来源。而数据的请求也主要是两种方式,即GET和POST。那么我们今天就通过基础爬虫知识来爬取大数据岗位的招聘情况,包括地区分布、薪资水平等。
要获取数据我们首先需要编写爬虫,这里我们就以为python来进行编写,数据的主要来源为前程无忧。代码如下:
#! -*- encoding:utf-8 -*- import requests import random # 要访问的目标页面 targetUrl = "http://httpbin.org/ip" # 要访问的目标HTTPS页面 # targetUrl = "https://httpbin.org/ip" # 代理服务器(产品官网 www.16yun.cn) proxyHost = "t.16yun.cn" proxyPort = "31111" # 代理验证信息 proxyUser = "username" proxyPass = "password" proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % { "host" : proxyHost, "port" : proxyPort, "user" : proxyUser, "pass" : proxyPass, } # 设置 http和https访问都是用HTTP代理 proxies = { "http" : proxyMeta, "https" : proxyMeta, } # 设置IP切换头 tunnel = random.randint(1,10000) headers = {"Proxy-Tunnel": str(tunnel)} resp = requests.get(targetUrl, proxies=proxies, headers=headers) print resp.status_code print resp.text
这里我们的示例是以随机ip访问网站进行编写的,因为在访问目标网站的过程中会涉及到反爬机制,所以一开始就做了代理ip策略,示例里使用的是动态转发模式的,这种模式的代理使用比较简单,更适合新手学习。在我们获取到的数据里面有些是需要进行去重的,去除掉无关数据就对数据进行清洗分析,但是分析过程比较繁琐,我们可以自己私下进行处理。
文中涉及的在获取数据过程中关于代理的使用,在这方面有需求和疑问的小伙伴可以咨询这里