Python爬虫入门教程 24-100 微医挂号网医生数据抓取
1. 写在前面
今天要抓取的一个网站叫做微医网站,地址为 https://www.guahao.com ,我们将通过python3爬虫抓取这个网址,然后数据存储到CSV里面,为后面的一些分析类的教程做准备。本篇文章主要使用的库为pyppeteer 和 pyquery
首先找到 医生列表页
https://www.guahao.com/expert/all/全国/all/不限/p5
这个页面显示有 75952 条数据 ,实际测试中,翻页到第38页,数据就加载不出来了,目测后台程序猿没有把数据返回,不过为了学习,我们忍了。
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
数据总过38页,量不是很大,咱只需要随便选择一个库抓取就行,这篇博客,我找了一个冷门的库
pyppeteer 在使用过程中,发现资料好少,很尴尬。而且官方的文档写的也不好,有兴趣的可以自行去看看。关于这个库的安装也在下面的网址中。
https://miyakogi.github.io/pyppeteer/index.html
最简单的使用方法,在官方文档中也简单的写了一下,如下,可以把一个网页直接保存为一张图片。
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'}) # 把网页生成截图
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') # 输入内容
await page.click('#password')
await page.type('#password', '123456')
await page.click('#login-submit')
await page.waitForNavigation()
# 设置浏览器窗口大小
await page.setViewport({
'width': 1350,
'height': 850
})
content = await page.content() # 获取网页内容
cookies = await page.cookies() # 获取网页cookies
3. 爬取页面
运行下面的代码,你就可以看到控制台不断的打印网页的源码,只要获取到源码,就可以进行后面的解析与保存数据了。如果出现控制不输出任何东西的情况,那么请把下面的
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. 解析数据
解析数据采用的是pyquery ,这个库在之前的博客中有过使用,直接应用到案例中即可。最终产生的数据通过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("正在存储数据....")
data = pd.DataFrame(self._data)
data.to_csv("微医数据.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() # 医院地址
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爬虫入门教程 24-100 微医挂号网医生数据抓取的更多相关文章
- Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy
1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...
- Python爬虫入门教程 27-100 微医挂号网专家团队数据抓取pyspider
1. 微医挂号网专家团队数据----写在前面 今天尝试使用一个新的爬虫库进行数据的爬取,这个库叫做pyspider,国人开发的,当然支持一下. github地址: https://github.com ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
- Python 东方财富网-股市行情数据抓取
东方财富网 股市行情数据抓取: http://quote.eastmoney.com/center/gridlist.html#hs_a_board 请求数据未入库处理,其中数据只存入数据文本,未做存 ...
- Python爬虫入门教程 37-100 云沃客项目外包网数据爬虫 scrapy
爬前叨叨 2019年开始了,今年计划写一整年的博客呢~,第一篇博客写一下 一个外包网站的爬虫,万一你从这个外包网站弄点外快呢,呵呵哒 数据分析 官方网址为 https://www.clouderwor ...
- Python爬虫入门教程 36-100 酷安网全站应用爬虫 scrapy
爬前叨叨 2018年就要结束了,还有4天,就要开始写2019年的教程了,没啥感动的,一年就这么过去了,今天要爬取一个网站叫做酷安,是一个应用商店,大家可以尝试从手机APP爬取,不过爬取APP的博客,我 ...
- Python爬虫入门教程 41-100 Fiddler+夜神模拟器+雷电模拟器配置手机APP爬虫部分
爬前叨叨 从40篇博客开始,我将逐步讲解一下手机APP的爬虫,关于这部分,我们尽量简化博客内容,在这部分中可能涉及到一些逆向,破解的内容,这部分尽量跳过,毕竟它涉及的东西有点复杂,并且偏离了爬虫体系太 ...
- Python爬虫入门教程 57-100 python爬虫高级技术之验证码篇3-滑动验证码识别技术
滑动验证码介绍 本篇博客涉及到的验证码为滑动验证码,不同于极验证,本验证码难度略低,需要的将滑块拖动到矩形区域右侧即可完成. 这类验证码不常见了,官方介绍地址为:https://promotion.a ...
随机推荐
- Vray
VRay是由chaosgroup和asgvis公司出品,中国由曼恒公司负责推广的一款高质量渲染软件.
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
- tf.contrib.slim arg_scope
缘由 最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下: with slim.arg_scope( [slim.conv2d, slim.separab ...
- URI is not registered ( Setting | Project Settings | Schemas and DTDs )
URI is not registered ( Setting | Project Settings | Schemas and DTDs ) 在idea中,当初手动第一次写spring配置文件的时候 ...
- [HACK] docker runtime 挂载宿主机目录
网上看到的很多所谓的挂载都是容器创建时期的挂载,而且参数都不清不楚,整理如下(--name别名自己加): docker run -v /src/path:/dest/path:rw ${IMAGE} ...
- Ubuntu出现卡logo、卡住、黑屏无法正常启动、屏幕和键盘背光无法调节等一系列问题?可能是NVIDIA显卡驱动没装好
也不知道是幸运还是不幸,我从一开始接触ubuntu就遇到这一系列的问题, 而且一直没有一个彻底解决的办法,搞得我无比头疼,也害得我重装了无数遍系统... 国际惯例,只按照个人习惯和喜好来写,对某些人来 ...
- 十八、泛型 l 注解 l Servlet3.0 l 动态代理 l 类加载器基础加强
l 泛型 l 注解 l Servlet3.0 l 动态代理 l 类加载器 泛型 1 回顾泛型类 泛型类:具有一个或多个泛型变量的类被称之为泛型类. public class A<T> { ...
- getMemory的经典例子
//NO.1:程序首先申请一个char类型的指针str,并把str指向NULL(即str里存的是NULL的地址,*str为NULL中的值为0),调用函数的过程中做了如下动作:1申请一个char类型的指 ...
- No Spring WebApplicationInitializer types detected on classpath 问题的一种解决办法
今天在idea中编译部署工程,tomcat报了这个错误: No Spring WebApplicationInitializer types detected on classpath 导致前端页面访 ...
- 通过一个小故事,理解 HTTPS 工作原理
本文摘录参考: 细说 CA 和证书(主要讲解 CA 的使用) 数字签名是什么?(简单理解原理) 深入浅出 HTTPS 工作原理(深入理解原理) HTTP 协议由于是明文传送,所以存在三大风险: 1.被 ...