python爬蟲思路

python2
爬蟲:從網頁上採取數據
爬蟲模塊:urllib,urllib2,re,bs4,requests,scrapy,xlml
1.urllib
2.request
3.bs4
4.正則re
5種數據類型
(1)數字Number
(2)字符串String
(3)列表List[] 中文在可迭代對象就是unicode對象
(4)元組Tuple()
(5)字典Set{}
爬蟲思路:
1.靜態 urlopen打開網頁------獲取源碼read
2.requests(模塊) get/post請求----獲取源碼 text()方法 content()方法(建議)
3.bs4 能夠解析HTML和XML
#-- coding:utf-8 --
from bs4 import BeautifulSoup
#1
#html="<div>2018.1.8 14:03</div>"
#soup=BeautifulSoup(html,'html.parser') #解析網頁
#print soup.div
#2從文件中讀取
html=''
soup=BeautifulSoup(open('index.html'),'html.parser')
print soup.prettify()
4.獲取所需信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章