Dowemo

learned the java crawler, in contrast, more likely to be simple and easy to understand. Like java, the request is encapsulated, and the python2 series uses urllib2, python3, and all of them.

The following example shows the simple use of python as an example of the first page.

Development environment: g1 ( integrated most commonly used python packages ) + python3 + ( development tools )


from urllib import request



# 创建request对象


req = request.Request('http://www.baidu.com')



# 添加数据


req.add_header('User-Agent', 'Mozilla/5.0')



# 发送请求获取结果


res = request.urlopen(req)



# 获取状态码


statusCode = res.getcode()


print(statusCode)



# 读取内容


content = res.read().decode('utf-8')


print(content)










Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs