requests库用户代理和ip代理使用

def get_html(url,params):
    try:
        uapools=[
                 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
                 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0',
                 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14'         
                ]
        ippools=[
                 '124.113.217.185:33755'
                 '113.120.63.10:40564',
                 '117.57.90.116:31028',
                 '116.239.107.198:40681',
                 '123.162.201.255:45684',
                 '115.211.231.157:48786',
                '125.123.127.97:38592',
                '124.113.192.128:47494',
                '171.211.80.94:45346',
                '193.112.111.90:51974'

                ]

        thisua=random.choice(uapools)
        headers={"User-Agent":thisua}

        thisip=random.choice(ippools)
        proxy_ip='http://'+thisip;  
        proxy_ips='https://'+thisip;
        proxy={'http':proxy_ip ,'https':proxy_ips}

        r=requests.get(url,headers=headers,params=params,proxies=proxy)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except Exception as e:
        print('Exception: ',e)
   

def main():
    try:
        url='https://www.baidu.com/s?'
        params={'wd':"love"}
        data=get_html(url,params=params)
        print(data[100:120])
    except Exception as e:
        print('Exception: ',e)

main()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值