Getting three reptiles (proxy and cookie)

1, agents

concept

  • Agent concept: a proxy server.
  • Role: accept requests == "request is forwarded.
  • Association between the agent and reptiles:
    • The mechanism may be used such that forward the request to the destination server receives a request corresponding to a change of ip.
  • Why use a proxy ip address change request?
    • Crawlers in a short time on the specified server launched a high frequency of requests, corresponding ip the request may be prohibited purpose server.
  • Agents some basics:
    • Agents degree of anonymity:
      • Transparent Proxy: The purpose server know you use a proxy mechanism and also know your real IP
      • Anonymous proxy: a proxy mechanism to know, but do not know your real ip
      • High anonymous proxy: a proxy do not know, do not know your real ip
    • Agent type:
      • http
      • https
  • Free proxy ip:
    • Fast Acting
    • West Temple Agent
    • www.goubanjia.com
    • Proxy Wizard

Example 1: Testing whether the agency will take effect

#准备:
    #代理ip和端口号
    #测试的过程
import requests
url = 'https://www.baidu.com/s?ie=utf-8&wd=ip'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers,proxies={'https':'112.195.96.115:32092'}).text
with open('./ip.html','w',encoding='utf-8') as fp:
    fp.write(page_text)
# 通过谷歌浏览器中 设置 代理设置 来设置代理ip 再从百度中搜索ip 观察是否变化

Example Two: a high frequency of request for initiating agent West Temple, ip is disabled so that the machine, and then using the agent pool of anti-climb agent solution

  • Agent pool building: is a list, the list is stored in the dictionary. Each key-value dictionary storage { 'http': 'ip: port'}
#线程池
all_ips = []
url = 'http://t.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=1&fa=0&fetch_key=&groupid=0&qty=30&time=1&pro=&city=&port=1&format=txt&ss=3&css=&dt=1&specialTxt=3&specialJson=&usertype=2'
page_text = requests.get(url,headers=headers).text
tree = etree.HTML(page_text)
ip_list = tree.xpath('//body//text()')[0].split('\n')
for ip in ip_list:
    dic = {
        'https':ip
    }
    all_ips.append(dic)
# 对西祠代理发起一个高频的请求 并使用线程池
from lxml import etree
import random
url = 'https://www.xicidaili.com/nn/%d'
ips = []
for page in range(1,3):
    new_url = format(url%page)
    page_text = requests.get(new_url,headers=headers,proxies=random.choice(all_ips)).text
    tree = etree.HTML(page_text)
    #在xpath表达式中一定不可以出现tbody标签
    tr_list = tree.xpath('//*[@id="ip_list"]//tr')[1:]
    for tr in tr_list:
        ip = tr.xpath('./td[2]/text()')[0]
        ips.append(ip)
print(len(ips))、cookie

2, cookie handling

请求头中重要的头信息
    User-Agent  
    Cookie
    Referer

#cookie的处理
    #手动处理
        #将cookie作用到headers中即可
#自动处理
    #session = requests.Session()
    #session的作用:
    #session可以像requests模块一样调用get和post进行请求发送
    #在进行请求发送的过程中如果产生了cookie,则cookie会被自动存储到session对象中。

Example 1: News Network data snowball crawling

# https://xueqiu.com/,对雪球网中的新闻数据进行爬取
#分析:
    #新闻数据是通过ajax动态加载出来的
    #捕获到ajax数据包中的url
    
    
session = requests.Session()
#第一次请求发送:为了捕获cookie且存储到session对象中
first_url = 'https://xueqiu.com/'
session.get(first_url,headers=headers)

#第二次请求发送:携带者cookie进行的请求发送
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=20358211&count=15&category=-1'
json_data = session.get(url=url,headers=headers).json()
json_data

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11999887.html