嘗試學習了將爬取的數據進行清洗,更新數據庫后。進行可視化。記錄一部分
使用pip進行安裝
jupyter安裝后再cmd下輸入 jupyter notebook
成功的話會跳轉到瀏覽器:
確實好用方便
在jupyter中編寫
有個庫是string中的。是標點符號的庫。 punctuation if not in punctuation
清洗數據后,更新 update方法 db.collection.update() 接收兩個參數 1. 更新哪個文件 2. 怎么改。
如下圖:
其他操作符
清洗后 可視化
Charts
例子:
import charts#數據series =[ { 'name':'OS X', 'data':[11], 'type':'column' }, {'name':'Ubuntu', 'data':[8], 'type':'column' }, {'name':'Windows', 'data':[12], 'type':'column' }, {'name':'Other', 'data':[29], 'type':'column' }]#使用charts繪畫charts.plot(series,show='inline',options=dict(title=dict(text='Charts test')))http://www.cnblogs.com/pangduzi/p/5889896.html
這個就寫的很詳細。我也記錄下,方便以后使用。
我也爬取了一會58二手數據。。。但是沒讓電腦跑那么久。 看到這位大佬也爬取了 http://www.cnblogs.com/pangduzi/p/5889952.html
學習了。
對這幾天學習的爬取做下總結: 更加熟練的使用BeautifulSoup了。結合谷歌瀏覽器的復制來獲取其分析路徑。 然后用其的select方法來獲取。
還有對其網頁進行分析。 使用多進程快些。多核的話多進程比多線程更快些。
先進入二手市場首頁,然后獲取所有二手分類
分析就是
Ul下的li下的b的a
ul.ym-submnu > li > b > a但是返回的只是后面的,并不是完整的url,那么拼接
# -*- coding: UTF-8 -*-import requestsfrom bs4 import BeautifulSoup#二手首頁,然后獲取每個二手分類的urlstart_url = 'http://cd.58.com/sale.shtml'def get_channel_urls(url): wb_data = requests.get(start_url) soup = BeautifulSoup(wb_data.content) links = soup.select('ul.ym-submnu > li > b > a') for link in links: page_url = 'http://cd.58.com/'+link.get('href') PRint(page_url)get_channel_urls(start_url)然后獲取每個分類也后的每個商品的url,并存入mongoDB
那么做個判斷。 比較有與沒有的差別 在于是否有商品,用的是td
就在for前面加個判斷
if soup.find('td','t'): for link in soup.select('td.t a.t'): item_link =link.get('href').split('?')[0] url_list.insert({'url':item_link}) print(item_link)else: pass然后第二個爬取,爬取每個商品的詳細信息
返回的是404頁面。那么做個判斷 404在 script標簽中有404就是灰跳轉到404
#爬取商品的詳細信息def get_item_info(url): wb_data = requests.get(url) soup = BeautifulSoup(wb_data.content) #有可能之前爬取的url突然刪除了或者已經交易出去了。 no_longer_exist = '404' in soup.find('script',type="text/javascript").get('src').split('/') if no_longer_exist: pass else: title = soup.title.text price = soup.select('span.price.c_f50')[0].text date = soup.select('.time')[0].text #有的有可能會沒有所有在后面做了個if else的判斷 ##content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-child(3) > div.su_con > span area = list(soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-of-type(3) > div.su_con > span > a:nth-of-type(1)')[0].text) if soup.find('span','c_25d') else None item_info.insert({'title':title,'price':price,'date':date,'area':area}) print(area)get_item_info('http://cd.58.com/diannao/23276725917860x.shtml')先有代理ip和端口 然后proxies={‘http’:那個代理} Request.get(url,headers=headers,proxies=proxies)
對于類似二手交易網站的爬取。 先爬取頻道,分析其url。 先進入一個頻道,然后分析其商品和需要的數據。使用beautifulsoup來解析,使用谷歌的復制方便些,也可以自己分析其結構。 然后可以進入這個商品的詳細信息分析。 使用MongoDB實在是有夠方便。。。。
一個爬蟲:爬取所有頻道url 一個爬取把放入的頻道url進行商品的爬取。 還可以寫個每個商品的詳細信息爬取。 使用多進程要快與多進程5-7倍。單核的電腦不要使用多進程。 可以寫個監視py 也就是簡單來說每隔一定時間查詢數據庫中的數據量。
就是多寫,多分析。多使用一些python方便的庫~
新聞熱點
疑難解答