python爬虫自动化_python自动化之爬虫原理及简单案例

【爬虫案例】动态地图里的数据如何抓取:以全国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')

python——flask常见接口开发(简单案例)

python——flask常见接口开发(简单案例)原创 大蛇王 发布于2019-01-24 11:34:06 阅读数 5208 收藏展开 版本:python3.5+ 模块:flask 目标:开发一个只 ...

爬虫之scrapy简单案例之猫眼

在爬虫py文件下 class TopSpider(scrapy.Spider): name = 'top' allowed_domains = ['maoyan.com'] start_urls = ...

使用python开发ansible自定义模块的简单案例

安装的版本ansible版本<=2.7,<=2.8是不行的哦 安装模块 pip install ansible==2.7 先导出环境变量 我们自定义模块的目录. 我存放的目录 export ...

python自动化之爬虫模拟登录

http://selenium-python.readthedocs.io/locating-elements.html ####################################### ...

python静态网页爬虫之xpath&lpar;简单的博客更新提醒功能)

直接上代码: #!/usr/bin/env python3 #antuor:Alan #-*- coding: utf-8 -*- import requests from lxml import e ...

爬虫之CrawlSpider简单案例之读书网

项目名py文件下 class DsSpider(CrawlSpider): name = 'ds' allowed_domains = ['dushu.com'] start_urls = ['htt ...

Python分布式爬虫原理

转载 permike 原文 Python分布式爬虫原理 首先,我们先来看看,如果是人正常的行为,是如何获取网页内容的. (1)打开浏览器,输入URL,打开源网页 (2)选取我们想要的内容,包括标题,作 ...

python写红包的原理流程包含random&comma;lambda其中的使用和见简单介绍

Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...

python模块之HTMLParser之穆雪峰的案例&lpar;理解其用法原理&rpar;

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之HTMLParser之穆雪峰的案例(理解其用法原理) #http://www.cnblog ...

随机推荐

树莓派pppoe

连接的网络是移动(铁通)的宽带,不同的宽带的dns需要修改. 1.首先安装pppoe包 apt-get install pppoe 2.然后,复制conf文件/etc/ppp/pppoe.conf: ...

通过goto语句学习if&period;&period;&period;else、switch语句并简单优化

goto语句在C语言中实现的就是无条件跳转,第二章一上来就介绍goto语句就是要通过goto语句来更加清楚直观的了解控制结构. 我理解的goto语句其实跟switch语句有相似之处,都是进行跳转.不同 ...

CentOS 安装Zookeeper-3&period;4&period;6 单节点

Dubbo 建议使用 Zookeeper 作为服务的注册中心. 注册中心服务器(192.168.3.71)配置,安装 Zookeeper: 1. 修改操作系统的/etc/hosts 文件中添加: #  ...

ijg库的使用的几点注意

ijg库(http://www.ijg.org/)是用于处理jpeg解码和压缩的库,最新版本为2014发布的版本,可以在官网中下载jpegsr9a.zip 使用vs中个nmake 进行编译,对于这个版 ...

常用Json

一般Json是页面与页面之间传递使用. Json用途        1 后台与前台数据交互,并且数据较复杂,如果数据单一,直接传递字符串,然后在前台用js分割就行. 2 webservice和html ...

利用 Google Chart API 生成二维码大小不一致

大小不一致是由于 chl  参数内容不一样导致的,而 chs 参数只能指定生成图片的大小,不能指定生成具体二维码大小. 比如:https://chart.googleapis.com/chart?ch ...

Python的内置方法——补充

七 __setitem__,__getitem__,__delitem__ class Foo: def __init__(self,name): self.name=name def __getit ...

Tesseract-OCR4&period;0版本在VS2015上的编译与运行&lpar;转&rpar;

最近刚开始接触识别库引擎方面的知识,由于项目中需要使用光学识别处理模块,在老师与朋友的推荐下,我开始接触tesseract光学识别库,在最开始从GitHub上下载的源代码进行编译的时候,出现了许多意想 ...

extract&lowbar;by&lowbar;one 根据二维数组中某字段来提取数组信息&comma;查看有无重复信息

public function tt(){ $param = array( array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70 ...

JavaScript中将html字符串转化为Jquery对象或者Dom对象

实例代码: $('

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值