python爬虫-基础入门-爬取整个网站《1》

python爬虫-基础入门-爬取整个网站《1》

描述:

  使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数据。

python代码如下:

 1 # -*- coding:  utf-8 -*-
 2 
 3 import urllib2
 4 
 5 def baiduNet() :
 6 
 7     request = urllib2.Request("http://www.baidu.com")
 8     response = urllib2.urlopen(request)
 9     netcontext = response.read()
10 
11     file = open("baidutext.txt","w")
12     file.write(netcontext)
13 
14 
15 if __name__ == "__main__" :
16     baiduNet()

执行后baidutext.txt数据,部分截图如下:

打开浏览器,访问百度,鼠标右键页面,查看源代码,如下:

使用代码爬取到的页面数据和源网站页面数据是一样的,爬取网页成功。

扫描二维码关注公众号,回复: 3924447 查看本文章

如有问题,欢迎纠正!!!

如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9903221.html

猜你喜欢

转载自www.cnblogs.com/Charles-Yuan/p/9903221.html