python 爬虫使用代理

一、示例代码

import requests

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "BIDUPSID=D6CC0B8A979744B0A83734A36249F0A7; PSTM=1655693507; sug=3; ORIGIN=0; sugstore=0; bdime=0; BD_UPN=12314753; BAIDUID=DD3C99E9CCA33FDD6E50DB5044FF014A:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36549_38407_38470_38439_38402_38468_38289_38380_37931_38383_26350_38394_38423_37881; BA_HECTOR=ah05840401208h8h810080681i2a8nd1n; BAIDUID_BFESS=DD3C99E9CCA33FDD6E50DB5044FF014A:FG=1; delPer=0; BD_CK_SAM=1; PSINO=6; ZFY=UXDGkfbQbrRHQYJJMsD8Z:B0JB:AWuKaTbzRtNE1m31ec:C; BD_HOME=1; shifen[534579497470_88487]=1680157547; BCLID=7704511630496961794; BCLID_BFESS=7704511630496961794; BDSFRCVID=-kkOJeC624JVx36f5e5wJCUuYZm4LncTH6aouRcN8BATD_Bjla-GEG0PBf8g0KAM-kGJogKK3mOTH4-F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; BDSFRCVID_BFESS=-kkOJeC624JVx36f5e5wJCUuYZm4LncTH6aouRcN8BATD_Bjla-GEG0PBf8g0KAM-kGJogKK3mOTH4-F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tRk8oK-atDvDqTrP-trf5DCShUFsa-rmB2Q-XPoO3Ktb8JACbxRfyMApLUTaW6jiW5cpoMbgylRp8P3y0bb2DUA1y4vp5hTe5gTxoUJ2XhrPDfcoqtnWhfkebPRiJ-r9Qg-JslQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0hD0wD5thj6PVKgTa54cbb4o2WbCQLUJM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F30h3QXP6-hnjy3b4fKIO85qvZbf7_LPTDXjDUypjpJh3RymJ42-39LPO2hpRjyxv4-nDyQtoxJpOJaK6x0P57HR7Wbh5vbURvDPug3-AJex5dtjTO2bc_5KnlfMQ_bf--QfbQ0hOhqP-jBRIEoC0XtI0hMCvPKITD-tFO5eT22-us-6vA2hcHMPoosIt6Q5b_bxP4WRJZ3-6aBRria-QCtMbUoqRHXnJi0btQDPvxBf7pK5reKl5TtUJMDnjjbborqt4be-oyKMnitKj9-pP2HlQrh459XP68bTkA5bjZKxtq3mkjbPbDfn028DKuj5LWj6O0eaDs-bbfHDnyL4Tq5njEDbIk-PnVeIufMPnZKxtqtJnZ-tPbJJLheD56yxbl5qFlhHOr2tJnWncKW-54KhuWElj2QhnIM4A_qtJ405OT0DFO0KJc0lCVjp3_hPJvyTtDXnO7tIrlXbrtXp7_2J0WStbKy4oTjxL1Db3JKjvMtIFtVJO-KKCMbK86jfK; ab_sr=1.0.1_YTYzMjVlMDBjY2U0OTM1MzAwMDE5YWM0MTAyMDQwMTViNmZlZTEzOWUzZDNiZjdjNGRiN2JmZWVhNmIwMWFmZDEwYzhkMDYzODg0ODYzYjk1NzM0ZjQ3YTY2NzQ5NWM3NDMxMjk1MTYzZDliNDA0Nzg3NWFlYjM0OGVhZDI2ZTMwZWVlYWRmY2JkOWE0MmJmZWI0YTI3ZTAwMGZlODAzOQ==; rsv_jmp_slow=1680160286911; COOKIE_SESSION=23_1_9_9_21_9_0_1_8_6_1_2_123_0_88_0_1680162654_1680157548_1680162566%7C9%231460702_99_1680157544%7C9; H_PS_645EC=62c6b2JVTgy55D9mlJuZC6O75n9yZYx9bNJB%2Fsgqy4xst4lTOwTvklsequg",
    "Host": "www.baidu.com",
}
proxies = {"http": f"http://127.0.0.1:6666",
           "https": f"https://xxx.xxx.xx.xx:9887"}
# 也可以使用socks5做代理
# proxy = 'socks5://{}:{}@{}:{}'.format(key, passwd, proxyHost, proxyPort)
# proxies = {"http": proxy,
#            "https": proxy}
requests.get(url="http://www.baidu.com", headers=headers, proxies=proxies)

二、一些可用代理网站

  • 云代理【http://www.ip3366.net/?stype=1&page=7】
  • 66ip【http://www.66ip.cn/2.html】
  • 快代理【https://www.kuaidaili.com/free/】

你可能感兴趣的:(#,python爬虫,爬虫)