今天要抓取的一個網(wǎng)站叫做微醫(yī)
網(wǎng)站,地址為 https://www.guahao.com
,我們將通過python3爬蟲抓取這個網(wǎng)址,然后數(shù)據(jù)存儲到CSV里面,為后面的一些分析類的教程做準(zhǔn)備。本篇文章主要使用的庫為pyppeteer
和 pyquery
為樂亭等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及樂亭網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站設(shè)計、樂亭網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
首先找到 醫(yī)生列表頁
https://www.guahao.com/expert/all/全國/all/不限/p5
這個頁面顯示有 75952條數(shù)據(jù) ,實際測試中,翻頁到第38頁,數(shù)據(jù)就加載不出來了,目測后臺程序猿沒有把數(shù)據(jù)返回,不過為了學(xué)習(xí),我們?nèi)塘恕?/p>
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
運行下面的代碼,你就可以看到控制臺不斷的打印網(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()
Python資源分享qun 784758214 ,內(nèi)有安裝包,PDF,學(xué)習(xí)視頻,這里是Python學(xué)習(xí)者的聚集地,零基礎(chǔ),進(jìn)階,都?xì)g迎
解析數(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() # 專家團隊
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()
Python資源分享qun 784758214 ,內(nèi)有安裝包,PDF,學(xué)習(xí)視頻,這里是Python學(xué)習(xí)者的聚集地,零基礎(chǔ),進(jìn)階,都?xì)g迎
總結(jié)一下,這個庫不怎么好用,可能之前沒有細(xì)細(xì)的研究過,感覺一般,你可以在多嘗試一下,看一下是否可以把整體的效率提高上去。
數(shù)據(jù)清單:
網(wǎng)站欄目:Python爬蟲新手教程:微醫(yī)掛號網(wǎng)醫(yī)生數(shù)據(jù)抓取
文章轉(zhuǎn)載:http://bm7419.com/article32/jdespc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、響應(yīng)式網(wǎng)站、網(wǎng)站營銷、全網(wǎng)營銷推廣、網(wǎng)站設(shè)計公司、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)