python USES a custom user agent method to crawl web pages

  • 2020-05-10 18:21:56
  • OfStack

This example shows how python USES custom user-agent to crawl web pages. Share with you for your reference. The details are as follows:

The python code below grabs the specified url content through urllib2 and USES the custom user-agent to prevent the site from blocking the collector


import urllib2
req = urllib2.Request('http://192.168.1.2/')
req.add_header('User-agent', 'Mozilla 5.10')
res = urllib2.urlopen(req)
html = res.read()

I hope this article has been helpful to your Python programming.


Related articles: