2019/6/21

今日内容:

1.Scrapy爬虫框架

2.微信机器人

3.MongoaDB可视化工具

def parse_area(sel£, response):

? print (response)

状取主员房源uJ标签对象

house_list = response. xpath(,//ul [•class=rsellListContent,,^],)

if house_list:

for house in house_list:

P房源名称

P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[l]/a

house_name = house. xpath(,. //dir [•class=rtitle,,,]/a/text 0 *). extract_£irst0 print(house_name)

^房源价格

P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[4]/dir[2]/dir[l]/span

house_cost = house. xpath(,. //dir [•class=JtotalPrice,,,]/span/text 0J). extract_£irst0 print(house_cost)

P房源单价

P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[4]/dir[2]/dir[2]/span

house_price = house. xpath(,. //dir [•class=JunitPrice,,,]/span/text 0J). extract_£irst0 print(house_price)

 

def parse (self, response):        # response返回的响应对象

#      print (response)

#      print (t)ve (response))

#获取文本

#      print (response, text)

#      print (response, url)

#      //*[§id=^position^]/dl[2]/dd/div[l]

                           

    
    
    

#获取城区列表urJ

    

area_list = response. xpath(’ //div[费data-role="ershoufang”/div/a’)

    
    
    

    
#遍历所有区域列表 for area in area_list: print(area)

 

 

;解析详情页^
def parse_detail(l
ext)
soup = BeautifulSoup(text, ’ lxml*) Jt print (soup)
3PPS 称
try:
name = soup, find(name=^span^, attrs= {"class": "title"}).text except Exception:
#若有异常,设置为汉一 name = None n pi ini
?好评率
try:
love = soup, find(name=’span’,attrs= {"class": ^love^}).text
except Exception: love = None
Jt print (love)

** Scarpy安装1-j使川 **
1、 pip3 install wheel
2、 pip3 install lxml
3、 pip3 install pyopenssl
4、 pip3 install
5、 安妓twistedfle:架
卜战 twisted
http://www.lfd.uci.edu/^gohlke/pythonlibs/#twisted 安酱卜•战好的twisted
pip3 install 11 ^\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
6、 pip3 install scrapy

 

** Scarpy使用 **
1、 进入终端cmd
-scrapy
C: \l'sers\administortra>scrapy Scrapy 1.6.0 - no active project
2、 创建scrapy项冃
1. 创建一个文件夹,专门川于存放scrapy项冃
-D:\Scrapy_prject
2. cmd终端输入命令
项冃名
scrapy startproject Spider—Project -会在D:\Scrapy_prject文件夹下会牛成一个文件 Spider_Project : Scrapy项冃文件

 

3.创述爬虫程斤
cd Spider_Project # 切换到scrapy项丨
#爬虫程序名称 H标M站域名
scrapy genspider baidu ww.baidu.com # 创爬虫程序

3、启动scrapy项目,执行爬虫程序
找到爬虫程序文件进行执行 scrapy runspider只能执行某个爬虫程序.py «切换到爬虫程序执行文件@录下
-cd D: \Scrapjr_pr ject\Spider_Prc(ject\Spider_Project\spiders -scrapy runspider baidu.py
根据爬虫名称找到相应的爬虫程序执行 scrapy crawl爬虫程序名称 «切换到项@@录下
-cd D:\Scrapy_prject\Spider_Project -scrapy crawl baidu

 

def parse_area(sel£, response):
? print (response)
状取主员房源uJ标签对象
house_list = response.xpath(,//ul [•class=,rsellListContent,,^],)
if house_list:
for house in house_list:
P房源名称
P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[l]/a
house_name = house.xpath(,.//dir [•class=,rtitle,,,]/a/text 0 *).extract_£irst0 print(house_name)
^房源价格
P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[4]/dir[2]/dir[l]/span
house_cost = house.xpath(,.//dir [•class=J,totalPrice,,,]/span/text 0J).extract_£irst0 print(house_cost)
P房源单价
P //*[^id=^leftContent ^j/ul/li[l]/dir/dir[4]/dir[2]/dir[2]/span
house_price = house.xpath(,.//dir [•class=J,unitPrice,,,]/span/text 0J).extract_£irst0 print(house_price)

 

 

scrapy.
#写终端的命令
# scrapy crawl baidu #执行baidu爬虫程序
# execute ([’’scrapy”, "crawl ”, ’’baidu’’])
#创建爬取链家网爬虫程序
/「" " " .." /y, .• • " /y, . • • "7 1
# executed scrapy , gen spider , lianjia f lianjia.com J)
#执行链家爬虫程序
execute (一scrapy crawl lianjia: split (一 一))

 

from scrapy.http, response, htrnl import
Htm
lResponse]
class LianjiaSpider(scrapy.Spider): name = ’ lianjia’
# M保留ijllflianjia.com的url allowed_doraains = [* lianjia.com'] ^
iY•放初始济求url
start一urls = U https://bj.lianjia.com/ershoufang/* ]
def parse (self, response): # response返l"l的响府对象
tt print (response) tt print (type (response)) tt获m文本 print(response, text) print(response, url)
pass

 

def parse (self, response): # response返回的响应对象
# print (response)
# print (t)ve (response))
#获取文本
# print (response, text)
# print (response, url)
# //*[§id=^position^]/dl[2]/dd/div[l]
#获取城区列表urJ
area_list = response.xpath(’ //div[费data-role="ershoufang”/div/a’)
#遍历所有区域列表 for area in area_list: print(area)
pass

 

一 Scrapy爬虫框架 发送请求 ---> 获取响应数据 ---> 解析数据 ---> 保存数据
** Scarpy框架介绍 **
1、引擎(EGINE) 引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。有关详细信息,请参见上面的数据流部分。
2、调度器(SCHEDULER) 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址
3、下载器(DOWLOADER) 用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的
4、爬虫(SPIDERS) SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求
5、项目管道(ITEM PIPLINES) 在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作 下载器中间件(Downloader Middlewares)位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response, 你可用该中间件做以下几件事:   (1) process a request just before it is sent to the Downloader (i.e. right before Scrapy sends the request to the website);   (2) change received response before passing it to a spider;   (3) send a new Request instead of passing received response to a spider;   (4) pass response to a spider without fetching a web page;   (5) silently drop some requests.
6、爬虫中间件(Spider Middlewares) 位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)
** Scarpy安装 ** 1、pip3 install wheel 2、pip3 install lxml 3、pip3 install pyopenssl 4、pip3 install pypiwin32 5、安装twisted框架 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted 安装下载好的twisted pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
6、pip3 install scrapy
** Scarpy使用 ** 1、进入终端cmd - scrapy C:\Users\administortra>scrapy Scrapy 1.6.0 - no active project
2、创建scrapy项目 1.创建一个文件夹,专门用于存放scrapy项目 - D:\Scrapy_prject 2.cmd终端输入命令 scrapy startproject Spider_Project( 项目名) - 会在 D:\Scrapy_prject文件夹下会生成一个文件 Spider_Project : Scrapy项目文件
3.创建爬虫程序 cd Spider_Project # 切换到scrapy项目目录下 # 爬虫程序名称 目标网站域名 scrapy genspider baidu www.baidu.com # 创建爬虫程序
3、启动scrapy项目,执行爬虫程序
# 找到爬虫程序文件进行执行 scrapy runspider只能执行某个 爬虫程序.py # 切换到爬虫程序执行文件目录下 - cd D:\Scrapy_prject\Spider_Project\Spider_Project\spiders - scrapy runspider baidu.py
# 根据爬虫名称找到相应的爬虫程序执行 scrapy crawl 爬虫程序名称 # 切换到项目目录下 - cd D:\Scrapy_prject\Spider_Project - scrapy crawl baidu

 

微信好友男女比率

from wxpy import Bot from pyecharts import Pie import webbrowser

# 实例化一个微信机器人对象 bot = Bot()

# 获取到微信的所有好友 friends = bot.friends()

# 设定男性\女性\位置性别好友名称 attr = ['男朋友', '女朋友', '人妖']

# 初始化对应好友数量 value = [0, 0, 0]

# 遍历所有的好友,判断这个好友是男性还是女性 for friend in friends:     if friend.sex == 1:         value[0] += 1     elif friend.sex == 2:         value[1] += 1     else:         value[2] += 1

# 实例化一个饼状图对象 pie = Pie('tank的好友们!')

# 图表名称str,属性名称list,属性所对应的值list,is_label_show是否现在标签 pie.add('', attr, value, is_label_show=True)

# 生成一个html文件 pie.render('friends.html')

# 打开html文件 webbrowser.open('friends.html')

 

微信好友地域分布

from wxpy import * from pyecharts import Map import webbrowser bot=Bot(cache_path=True)

friends=bot.friends()

area_dic={}#定义一个字典,用来存放省市以及省市人数 for friend in friends:     if friend.province not in area_dic:         area_dic[friend.province]=1     else:         area_dic[friend.province]+=1

attr = area_dic.keys() value = area_dic.values()

 

map = Map("好朋友们的地域分布", width=1200, height=600) map.add(     "好友地域分布",     attr,     value,     maptype='china',     is_visualmap=True, #结合体VisualMap

) #is_visualmap -> bool 是否使用视觉映射组件 # map.render('area.html')

webbrowser.open("area.html")

pip3 install echarts-countries-pypkg pip3 install echarts-china-provinces-pypkg pip3 install echarts-china-cities-pypkg pip3 install echarts-china-counties-pypkg pip3 install echarts-china-misc-pypkg
posted @ 2019-06-21 08:57  邵贤伟  阅读(193)  评论(0编辑  收藏  举报