ວິທີໃຊ້ proxy ໃນ Python Requests (HTTP/HTTPS & Session pooling)
Updated:
ໄລບຣາຣີ requests ຂອງ Python ຮອງຮັບ proxy ຂອງ VinaProxy ທີ່ມີການຢືນຢັນຕົວຕົນຜ່ານ dictionary proxies ທີ່ຈັບຄູ່ທັງ scheme 'http' ແລະ 'https' ໄປຫາ http://user:pass@ip:port. ການເຊື່ອມຕໍ່ dictionary ດັ່ງກ່າວກັບ requests.Session() ແບບຖາວອນຈະຊ່ວຍນຳໃຊ້ TCP socket ເດີມຄືນໃໝ່ ເພື່ອການດຶງຂໍ້ມູນເວັບ (web scraping) ທີ່ວ່ອງໄວ ແລະ ຫຼຸດຜ່ອນ overhead.
proxies = {'http': 'http://user:pass@ip:port', 'https': 'http://user:pass@ip:port'}- 1 IP₫40.000
- 5+ IP₫35.000
- 10+ IP₫25.000
- 20+ IP₫25.000
- 50+ IP₫25.000
- 100+ IP₫25.000
- 200+ IP₫25.000
- 500+ IP₫25.000
- 1000+ IP₫22.000
- 2000+ IP₫20.000
- 1 IP₫40.000
- 5+ IP₫35.000
- 10+ IP₫25.000
- 20+ IP₫25.000
- 50+ IP₫25.000
- 100+ IP₫25.000
- 200+ IP₫25.000
- 500+ IP₫25.000
- 1000+ IP₫22.000
- 2000+ IP₫20.000
ການຈັດຮູບແບບ proxies dictionary & ການນຳໃຊ້ Session ຄືນໃໝ່
ການຮຽກໃຊ້ requests.get() ໃນທຸກ URL ຈະສ້າງການເຊື່ອມຕໍ່ TCP ໃໝ່ ແລະ TLS handshake ໃໝ່ໃນແຕ່ລະຄັ້ງ. ການຜູກ proxy ຂອງທ່ານເຂົ້າກັບ session = requests.Session() ຈະຮັກສາ proxy tunnel ໃຫ້ເປີດຢູ່ຕະຫຼອດການຮ້ອງຂໍທີ່ຕໍ່ເນື່ອງກັນ.
| ພາຣາມິເຕີ Requests | Syntax ທີ່ແນະນຳ | ຈຸດປະສົງທາງເຕັກນິກ |
|---|---|---|
ຄີ 'http' | 'http://user:pass@ip:port' | ສົ່ງຄຳຮ້ອງຂໍ HTTP ທຳມະດາຜ່ານ proxy |
ຄີ 'https' | 'http://user:pass@ip:port' | ໃຊ້ scheme http:// ເພື່ອໃຫ້ urllib3 ສ້າງ HTTP CONNECT tunnel ສຳລັບ TLS |
tuple timeout | timeout=(5, 15) | connect timeout 5 ວິນາທີ ແລະ read timeout 15 ວິນາທີ ຊ່ວຍປ້ອງກັນບໍ່ໃຫ້ worker thread ຄ້າງ |
| ການໝຸນວຽນ Pool | itertools.cycle(proxy_list) | ໝຸນວຽນຢ່າງສະໝໍ່າສະເໝີທົ່ວກຸ່ມ dedicated static IP ຂອງທ່ານ |
ການຫຼີກລ່ຽງຂໍ້ຜິດພາດທົ່ວໄປຂອງ urllib3 & ProxyError
- ໃຫ້ໃຊ້
http://(ບໍ່ແມ່ນhttps://) ເປັນ proxy URL scheme ສຳລັບທັງຄີ dictionary'http'ແລະ'https'ສະເໝີ ເວັ້ນເສຍແຕ່ພອດ proxy ຂອງທ່ານຈະຕ້ອງການ TLS-over-TLS ໂດຍສະເພາະ - URL-encode ຕົວອັກສອນພິເສດໃນລະຫັດຜ່ານໂດຍໃຊ້
urllib.parse.quote(password, safe='') - ຂໍ້ຜິດພາດ
407 Proxy Authentication Requiredໝາຍຄວາມວ່າ ຊື່ຜູ້ໃຊ້ ຫຼື ລະຫັດຜ່ານໃນ URL string ຂອງທ່ານບໍ່ກົງກັບຂໍ້ມູນປະຈຳຕົວໃນ dashboard ຂອງທ່ານ - ເຮັດວຽກໄດ້ແບບດຽວກັນກັບ
httpx.Client(proxy='http://user:pass@ip:port')ແລະaiohttpsession.get(url, proxy=...)
ເທື່ອລະຂັ້ນຕອນ
- 1
ແປງ ip:port:user:pass ເປັນ URL syntax
ໃຊ້ Proxy Format Converter ຂອງພວກເຮົາເພື່ອປ່ຽນລາຍການ VinaProxy ຂອງທ່ານໃຫ້ເປັນ string
http://user:pass@ip:port. - 2
ກຳນົດ proxies dictionary
ກຳນົດ
proxies = {'http': 'http://user:pass@ip:port', 'https': 'http://user:pass@ip:port'}ໃນສະຄຣິບ Python ຂອງທ່ານ. - 3
ເຊື່ອມຕໍ່ກັບ instance ຂອງ requests.Session()
ສ້າງ instance ດ້ວຍ
s = requests.Session()ແລະ ຮຽກໃຊ້s.proxies.update(proxies)ສຳລັບ pooling ແບບKeep-Aliveທີ່ຕໍ່ເນື່ອງ. - 4
ກວດສອບ exit IP ດ້ວຍຄຳຮ້ອງຂໍທົດສອບ
ຣັນ
print(s.get('https://vinaproxy.com/api/my-ip', timeout=10).json())ເພື່ອຢືນຢັນ IP ຂອງ proxy ທີ່ກຳລັງໃຊ້ງານຢູ່.
ຄຳຖາມທີ່ຖາມເລື້ອຍໆ
ເປັນຫຍັງຄີ 'https' ຈຶ່ງໃຊ້ 'http://user:pass@ip:port'?
ຄຳນຳໜ້າ http:// ບອກ urllib3 ວິທີເຊື່ອມຕໍ່ກັບເຊີບເວີ proxy ເອງ (ໂດຍໃຊ້ HTTP CONNECT ມາດຕະຖານ), ໃນຂະນະທີ່ traffic ແບບ end-to-end ຂອງທ່ານໄປຍັງເວັບໄຊ https:// ປາຍທາງຍັງຄົງຖືກເຂົ້າລະຫັດ TLS ຢ່າງສົມບູນ.
ຂ້ອຍຈະໝຸນວຽນ 25 static proxy ໃນແຕ່ລະຄຳຮ້ອງຂໍໃນ Python ໄດ້ແນວໃດ?
ໂຫຼດ URL proxy ທັງ 25 ໂຕຂອງທ່ານເຂົ້າໃນ pool = itertools.cycle(PROXIES) ແລະ ສົ່ງ proxies={'http': p, 'https': p} ໃນແຕ່ລະຄຳຮ້ອງຂໍ ພ້ອມກັບການຈຳກັດອັດຕາການຮ້ອງຂໍທີ່ເໝາະສົມຕໍ່ IP.
VinaProxy ມີການວັດແທກ bandwidth ໃນສະຄຣິບ scraping ຂອງ Python ຫຼື ບໍ່?
ບໍ່, ທຸກແພັກເກດ Dedicated IPv4 ແລະ Static ISP ລວມມີ traffic ປະຈຳເດືອນແບບບໍ່ຈຳກັດ.
ສ້າງ pool ສຳລັບ Python scraping ແບບບໍ່ຈຳກັດ data
proxy ແບບ Dedicated IPv4 ແລະ Static ISP ພ້ອມໃຊ້ງານທັນທີໃນ 7 ປະເທດ.
ຊື້ Python Proxies