【爬蟲案例】動態地圖里的數據如何抓取:以全國PPP綜合信息平台網站為例 http://mp.weixin.qq.com/s/BXWTf5hmq8vp91ZvgaphEw
【爬蟲案例】動態頁面的抓取!以東方財富網基金行情數據為例 http://mp.weixin.qq.com/s/bbw5caz4EfJn5mwbDMVfuQ
【爬蟲案例】獲取歷史天氣數據 http://mp.weixin.qq.com/s/MlqJUuH0JjTujMzGJp_7kw
【爬蟲案例】電影票房數據抓取 https://mp.weixin.qq.com/s/UgH53P86Y0nfY-67EDQ8wA
#####http://www.lishi.tianqi.com/yangzhong/201407.html
#####http://lishi.tianqi.com/yangzhong/201407.html
#####www.cbooo.cn/year?year=2016
#####www.cpppc.org:8082/efmisweb/ppp/projectLibrary/toPPPMap.do
#####fundact.eastmoney.com/banner/gp.html?from=groupmessage&isappinstalled=0
#
#http://lishi.tianqi.com/yangzhong/201407.html
##################################################################
##############爬取票房紀要
#####www.cbooo.cn/year?year=2016
1、確認搜的票房數據在代碼里(Ctrl+F搜索出來)搜索關鍵字:如"美人魚",是否在頁面上
2、模板(對於數據在頁面上適用):獲取頁面/解析網頁
3、找到數據在哪?定位數據首選用id定位
4、返回列表的話找對應的項
#########采用解析器:'lxml',解析頁面;也可以用html.parse解析器
分析數據在哪個框里面,這是一個table,定位方式首選用id定位
soup.find_all 找到所有table,限制條件為id=tbContent
里面每一個tr代表一行,一個電影即為一行,找到所有tr標簽
td表示當中的每一個單元,找出當中第一個中的a標簽中的title屬性即為需要的電影名稱
dd與dl是現在很少用的標簽,表示為定義式,有點類似於字典
import requests ############獲取頁面
from bs4 import BeautifulSoup ############解析網頁
year=2017
url='http://www.cbooo.cn/year?year='+str(year)
rawhtml=requests.get(url).content
print(type(rawhtml))
soup=BeautifulSoup(rawhtml,'lxml') #########采用解析器:'lxml',解析頁面;也可以用html.parse解析器
###soup.select('dl.dltext dd')
###有快捷的方式:能把所有標簽去掉,soup.select('dl.dltext dd')[0].get_text()
def getYear(year):
#year=2017
url='http://www.cbooo.cn/year?year='+str(year)
rawhtml=requests.get(url).content
#print(type(rawhtml))
soup=BeautifulSoup(rawhtml,'lxml') #########采用解析器:'lxml',解析頁面;也可以用html.parse解析器
#print(type(soup))
return soup
def getInfo(url):
rawhtml=requests.get(url).content
soup=BeautifulSoup(rawhtml,'lxml')
return soup
print(type(soup))
movies_table=soup.find_all('table',{'id':"tbContent"})[0] ####用find_all()方法,通過table標簽,加上字典參數,屬性為id=tbContent
movies=movies_table.find_all('tr')
moviename=[movie.find_all('td')[0].a.get('title') for movie in movies[1:]]
movielink=[movie.find_all('td')[0].a.get('href') for movie in movies[1:]]
movietype=[movie.find_all('td')[1].string for movie in movies[1:]]
movieboxoffice =[int(movie.find_all('td')[2].string) for movie in movies[1:]]
#moviedirector=[getInfo(url).find_all('dl',{'class':'dltext'})[0].find_all('dd')[0].a.get('title') for url in movielink]
moviedirector=[getInfo(url).select('dl.dltext dd')[0].get_text() for url in movielink]
############轉成數據框&統計分析
import pandas as pd
df=pd.DataFrame({'names':moviename,'types':movietype,'boxoffice':movieboxoffice,'link':movielink,'directors':moviedirector})
import numpy as np
df.groupby('types').agg({'boxoffice':["count","mean"]})
#############寫到文件中
df.to_csv(r'C:\Users\Administrator\Desktop\電影.csv')
標簽是div,div在html中意思為一個塊集
確認html頁面真的存在代碼中
確認數據在代碼中,即好爬,如果不在代碼中,用js進行渲染,即不好爬
再看有沒有翻頁,沒有翻頁,即OK
這里以一個電影評分的網站為例,介紹數據抓取的基本流程和方法。
標准配置:
--requests:抓取網址的HTML內容
--BeautifulSoup:解析HTML源碼,提供方便的查詢接口
--re:正則表達式,通過描述規則從字符中提取需要的數據
(這里不作介紹)
import requests ########獲取頁面
from bs4 import BeautifulSoup #######解析網頁
url='http://www.cbooo.cn/year?year=2016'
rawhtml=requests.get(url).content #######獲取內容
##################################################################
##############爬取天氣紀要
###############http://www.tianqihoubao.com/weather/top/shenzhen.html
##############數據抓取:
##############某些情況下需要從網絡抓取數據,比如輿情監控需要抓取相關的新聞內容;
##############判斷天氣原因是否對超市的銷量有影響時,除了已有的銷量數據外還需要從
##############網絡抓取每日的天氣數據
1、下載的url數據
2、在谷歌瀏覽器右鍵:檢查,找到每一行數據在不在網頁代碼中,找到整個下載數據是個table,tblite_go
3、一頁一頁加載時,發現問題:網址未發生變化,沒有刷新
1)打開network,點擊每一頁時發現Request URL不一致,此時表明為異步加載;
2)將不同頁的鏈接復制出來,查看區別;
3)找到規律,將鏈接查看,即對應數據;
4)由於r.content為亂碼,r.text為中文格式;
5)解析;
6)每一頁寫入;
import requests ############獲取頁面
from bs4 import BeautifulSoup ############解析網頁
url='http://www.tianqihoubao.com/weather/top/shenzhen.html'
rawhtml=requests.get(url).content
weatherhtml=BeautifulSoup(rawhtml,'lxml')
dateset=[weather.find_all('td')[1].b.a.string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
dayweatherset=[weather.find_all('td')[2].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
daywindset=[weather.find_all('td')[3].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
daytempset=[weather.find_all('td')[4].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
nightweatherset=[weather.find_all('td')[5].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
nightwindset=[weather.find_all('td')[6].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
nighttempset=[weather.find_all('td')[7].string for weather in weatherhtml.find_all('table')[0].find_all('tr')[2:]]
import pandas as pd
df=pd.DataFrame({'日期':dateset,'白天天氣':dayweatherset,'白天風向':daywindset,'白天溫度':daytempset,'晚上天氣':nightweatherset,'晚上風向':nightwindset,'晚上溫度':nighttempset})
import numpy as np
df.to_csv(r'C:\Users\Administrator\Desktop\天氣.csv')