requests模块爬虫使用
今天使用requests,爬取"http://langlang2017.com/
立马上代码!
#第一步使用代理
#都是键值对,而且有HTTP 和HTTPS的代理
import requests
proxy={
"HTTP": "113.3.152.88:8118",
"HTTPS":"219.234.5.128:3128",
}
#伪装请求头
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/70.0.3538.110 Safari/537.36",
}
#第一种发起请求 使用get方式 网址 代理
response=requests.request("get","http://langlang2017.com/",proxies=proxy,headers=headers)
# 返回状态码
print(response)
# 返回字节流内容 解码为utf-8
print(response.content.decode("utf-8"))
# 返回返回文本内容
print(response.text)
#第二种发起请求
response1=requests.get("http://langlang2017.com/",headers=headers,proxies=proxy)
print(response1)# 返回状态码
# 返回字节流内容 解码为utf-8
print(response1.content.decode("utf-8"))
# 返回返回文本内容
print(response1.text)
爬虫其实很简单,难的是应对反爬机制!要灵活应对!更新完所有基础就,就开始反反爬虫!