Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

User Agent of python crawler

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

In the process of learning crawlers, sometimes do not use the head in the use of python crawler script just crawled twice, just a test can not open this page, at first has been confused, until later know that python does crawlers when the default user agent is the large version of python, python2.7. User-Agent: Python-urllib/2.7;python3.5. User-Agent: Python-urllib/3.5

Let's do an experiment:

The python code is as follows:

Python2

Import urllib2url = "http://www.baidu.com/"request = urllib2.Request (url) response = urllib2.urlopen (request) print (response.read ())

Python3

From urllib import requesturl = "http://www.baidu.com/"req = request.Request (url) response = request.urlopen (req) print (response.read () .decode ()

We open fiddler, run the completion code, and then look at our data on fiddler.

It's obviously the version of python.

Therefore, no matter what we climb when we learn about crawlers, it is best to add this header message to the code.

Let's add a header to the code.

From urllib import requestheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64"} url = "http://www.baidu.com/"req = request.Request (url,headers=headers) response = request.urlopen (req) print (response.read (). Decode ())

The result of grabbing the package is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report