Python微醫(yī)掛號網(wǎng)醫(yī)生數(shù)據(jù)抓取
1. 寫在前面
今天要抓取的一個網(wǎng)站叫做微醫(yī)網(wǎng)站,地址為 https://www.guahao.com ,我們將通過python3爬蟲抓取這個網(wǎng)址,然后數(shù)據(jù)存儲到CSV里面,為后面的一些分析類的教程做準(zhǔn)備。本篇文章主要使用的庫為pyppeteer 和 pyquery
首先找到 醫(yī)生列表頁
https://www.guahao.com/expert/all/全國/all/不限/p5
這個頁面顯示有 75952 條數(shù)據(jù) ,實際測試中,翻頁到第38頁,數(shù)據(jù)就加載不出來了,目測后臺程序猿沒有把數(shù)據(jù)返回,不過為了學(xué)習(xí),我們?nèi)塘恕?/p>
2. 頁面URL
https://www.guahao.com/expert/all/全國/all/不限/p1
https://www.guahao.com/expert/all/全國/all/不限/p2
...
https://www.guahao.com/expert/all/全國/all/不限/p38
數(shù)據(jù)總過38頁,量不是很大,咱只需要隨便選擇一個庫抓取就行,這篇博客,我找了一個冷門的庫
pyppeteer 在使用過程中,發(fā)現(xiàn)資料好少,很尷尬。而且官方的文檔寫的也不好,有興趣的可以自行去看看。關(guān)于這個庫的安裝也在下面的網(wǎng)址中。
https://miyakogi.github.io/pyppeteer/index.html
最簡單的使用方法,在官方文檔中也簡單的寫了一下,如下,可以把一個網(wǎng)頁直接保存為一張圖片。
import asyncio from pyppeteer import launch async def main(): browser = await launch() # 運行一個無頭的瀏覽器 page = await browser.newPage() # 打開一個選項卡 await page.goto('http://www.baidu.com') # 加載一個頁面 await page.screenshot({'path': 'baidu.png'}) # 把網(wǎng)頁生成截圖 await browser.close() asyncio.get_event_loop().run_until_complete(main()) # 異步
我整理了下面的一些參考代碼,你可以 做一些參考。
browser = await launch(headless=False) # 可以打開瀏覽器 await page.click('#login_user') # 點擊一個按鈕 await page.type('#login_user', 'admin') # 輸入內(nèi)容 await page.click('#password') await page.type('#password', '123456') await page.click('#login-submit') await page.waitForNavigation() # 設(shè)置瀏覽器窗口大小 await page.setViewport({ 'width': 1350, 'height': 850 }) content = await page.content() # 獲取網(wǎng)頁內(nèi)容 cookies = await page.cookies() # 獲取網(wǎng)頁cookies
3. 爬取頁面
運行下面的代碼,你就可以看到控制臺不斷的打印網(wǎng)頁的源碼,只要獲取到源碼,就可以進(jìn)行后面的解析與保存數(shù)據(jù)了。如果出現(xiàn)控制不輸出任何東西的情況,那么請把下面的
await launch(headless=True)
修改為 await launch(headless=False)
import asyncio from pyppeteer import launch class DoctorSpider(object): async def main(self, num): try: browser = await launch(headless=True) page = await browser.newPage() print(f"正在爬取第 {num} 頁面") await page.goto("https://www.guahao.com/expert/all/全國/all/不限/p{}".format(num)) content = await page.content() print(content) except Exception as e: print(e.args) finally: num += 1 await browser.close() await self.main(num) def run(self): loop = asyncio.get_event_loop() asyncio.get_event_loop().run_until_complete(self.main(1)) if __name__ == '__main__': doctor = DoctorSpider() doctor.run()
4. 解析數(shù)據(jù)
解析數(shù)據(jù)采用的是pyquery ,這個庫在之前的博客中有過使用,直接應(yīng)用到案例中即可。最終產(chǎn)生的數(shù)據(jù)通過pandas保存到CSV文件中。
import asyncio from pyppeteer import launch from pyquery import PyQuery as pq import pandas as pd # 保存csv文件 class DoctorSpider(object): def __init__(self): self._data = list() async def main(self,num): try: browser = await launch(headless=True) page = await browser.newPage() print(f"正在爬取第 {num} 頁面") await page.goto("https://www.guahao.com/expert/all/全國/all/不限/p{}".format(num)) content = await page.content() self.parse_html(content) print("正在存儲數(shù)據(jù)....") data = pd.DataFrame(self._data) data.to_csv("微醫(yī)數(shù)據(jù).csv", encoding='utf_8_sig') except Exception as e: print(e.args) finally: num+=1 await browser.close() await self.main(num) def parse_html(self,content): doc = pq(content) items = doc(".g-doctor-item").items() for item in items: #doctor_name = item.find(".seo-anchor-text").text() name_level = item.find(".g-doc-baseinfo>dl>dt").text() # 姓名和級別 department = item.find(".g-doc-baseinfo>dl>dd>p:eq(0)").text() # 科室 address = item.find(".g-doc-baseinfo>dl>dd>p:eq(1)").text() # 醫(yī)院地址 star = item.find(".star-count em").text() # 評分 inquisition = item.find(".star-count i").text() # 問診量 expert_team = item.find(".expert-team").text() # 專家團(tuán)隊 service_price_img = item.find(".service-name:eq(0)>.fee").text() service_price_video = item.find(".service-name:eq(1)>.fee").text() one_data = { "name": name_level.split(" ")[0], "level": name_level.split(" ")[1], "department": department, "address": address, "star": star, "inquisition": inquisition, "expert_team": expert_team, "service_price_img": service_price_img, "service_price_video": service_price_video } self._data.append(one_data) def run(self): loop = asyncio.get_event_loop() asyncio.get_event_loop().run_until_complete(self.main(1)) if __name__ == '__main__': doctor = DoctorSpider() doctor.run()
總結(jié)一下,這個庫不怎么好用,可能之前沒有細(xì)細(xì)的研究過,感覺一般,你可以在多嘗試一下,看一下是否可以把整體的效率提高上去。
數(shù)據(jù)清單:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
pycharm中:OSError:[WinError?1455]頁面文件太小無法完成操作問題的多種解決方法
這篇文章主要給大家介紹了關(guān)于pycharm中:OSError:[WinError?1455]頁面文件太小無法完成操作問題的多種徹底解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-02-02Python在不同目錄下導(dǎo)入模塊的實現(xiàn)方法
下面小編就為大家?guī)硪黄狿ython在不同目錄下導(dǎo)入模塊的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10對numpy中的transpose和swapaxes函數(shù)詳解
今天小編就為大家分享一篇對numpy中的transpose和swapaxes函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Python drop方法刪除列之inplace參數(shù)實例
這篇文章主要介紹了Python drop方法刪除列之inplace參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python pandas軸旋轉(zhuǎn)stack和unstack的使用說明
這篇文章主要介紹了Python pandas軸旋轉(zhuǎn)stack和unstack的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03