Python combat: How to hide your reptile identity

Source: Internet
Author: User

Using crawlers to access the site, you need to hide their identities as much as possible, in case of being blocked by the server, in the work of the project, we have 2 ways to achieve this goal, are delayed access and dynamic agents, and then we will explain these two ways

1. Delayed access

In the sense of name, time-lapse access is to set an access cycle when visiting a website, accessed every few seconds, in a way that is more like an artificial visit to the site


   
  
  1. Import Time
  2. Import Urllib.request
  3. CNT = 0
  4. #隐藏自己爬虫的身份的第一种策略是设置访问周期, making the program more like an artificial access
  5. while True: #每隔5秒钟访问一次百度网
  6. url = "https://www.baidu.com" #设置url地址
  7. param = {} #设置参数, parameter is dictionary
  8. param = Urllib.parse.urlencode (param). Encode (' utf_8 ') #将参数以utf-encoded by 8 encoding
  9. req = urllib.request.Request (URL, param)
  10. #设置header的User the-agent property, impersonate the request is sent by the Fox Fire Browser, that is, the spoofing server is sent by the sender is not sent by the program
  11. Req.add_header ("User-agent", "mozilla/5.0" (Macintosh; Intel Mac OS X 10.12; rv:53.0) gecko/20100101 firefox/53.0 ")
  12. Response = Urllib.request.urlopen (req) #访问网络
  13. html = response.read () #读取响应的结果
  14. result = Html.decode ("Utf-8") #按照utf-8 encoding for decoding
  15. if result! = "":
  16. CNT + = 1
  17. Print ("%s attacks Baidu Net" %cnt)
  18. Time.sleep (5) #程序睡眠5秒钟
Operation Result:

Visit Baidu net every 5 seconds



2. Dynamic Agent

Use proxy server to access the site, this method is very overbearing, can simulate different server access to the site, but also the most recommended way, we can find a free proxy server IP on Baidu online


   
  
  1. Import Urllib.request
  2. Import Random
  3. IPList = [' 119.6.144.73:81 ', ' 183.203.208.166:8118 ', ' 111.1.32.28:81 '] #定义多个代理IP, proxy ip can be searched online for free
  4. CNT = 0
  5. #隐藏自己爬虫的身份的第二种策略是使用代理, which means to simulate multiple server accesses
  6. while True: #使用代理服务器不停的访问百度网
  7. Proxy_support = Urllib.request.ProxyHandler ({' http ': Random.choice (IPList)}) #定义一个代理对象, using a random IP
  8. Opener = Urllib.request.build_opener (Proxy_support)
  9. Opener.add_handlers = [("User-agent", "mozilla/5.0" (Macintosh; Intel Mac OS X 10.12; rv:53.0) gecko/20100101 firefox/53.0 ")]
  10. Urllib.request.install_opener (opener)
  11. Response = Urllib.request.urlopen ("https://www.baidu.com") #访问网络
  12. html = response.read () #读取响应的结果
  13. result = Html.decode ("Utf-8") #按照utf-8 encoding for decoding
  14. if result! = "":
  15. CNT + = 1
  16. Print ("%s attacks Baidu Net" %cnt)
Operation Result:

Keep attacking the Baidu network






Python combat: How to hide your reptile identity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.