Python Requests爬虫求获取关键词页面
之前有分享过一些python爬取网站的不同方式,今天就重点来给大家介绍下使用这Python Requests爬虫如何进行数据的获取,这里我们就以求取关键词页面为学习目标。首先需求是爬取搜狗首页的页面数据,在这个过程中我们会使用的一些爬虫技术,比如使用UA伪装请求页面数据。面对网站封IP的情况会在爬取过程中加上代理IP的使用。代理的选择比较这里直接使用的是由亿牛云提供的爬虫代理,相对传统的api获取方式要方便的多,使用也有示例可以参考,这里我们在实践中给大家展示下使用的方式。爬取搜狗页面数据的代码示例如下:
#! -*- encoding:utf-8 -*-
import requests
import random
# 要访问的目标页面
targetUrl = "https://123.sogou.com"
# 要访问的目标HTTPS页面
# targetUrl = "https://123.sogou.com"
# 代理服务器(产品官网 www.16yun.cn)
proxyHost = "t.16yun.cn"
proxyPort = "31111"
# 代理验证信息
proxyUser = "16UXDXBJ"
proxyPass = "098504 "
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
在整个爬取过程中我们使用的ua要随机更换,不能一直使用一个进行访问,这样很容易被目标网站识别,也会对代理IP的使用效果有一定的影响。在使用代理过程中有其他需求的可以详细去官网咨询https://www.16yun.cn/。
234234234234 qweqweqwe 1231232121 ewrwerwerwerwerwerwerwe 12312312321 123123123 777777777777 11111111111111 12321321312 1231231231
页:
[1]