python爬蟲-基礎入門-爬取整個網站《1》

python爬蟲-基礎入門-爬取整個網站《1》

描述:html

  使用環境:python2.7.15 ,開發工具:pycharm,現爬取一個網站頁面(http://www.baidu.com)全部數據。python

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數據,部分截圖以下:python爬蟲

 

打開瀏覽器,訪問百度,鼠標右鍵頁面,查看源代碼,以下:python2.7

 

使用代碼爬取到的頁面數據和源網站頁面數據是同樣的,爬取網頁成功。工具

 

若有問題,歡迎糾正!!!開發工具

若有轉載,請標明源處:https://www.cnblogs.com/Charles-Yuan/p/9903221.html網站

相關文章
相關標籤/搜索