抓取大众点评首页左侧信息,如图:

我们要实现把中文名字都存到mongodb,而每个链接存入redis数据库。

因为将数据存到mongodb时每一个信息都会有一个对应的id,那样就方便我们存入redis可以不出错。

# -*- coding: utf-8 -*-
import re
from urllib.request import urlopen
from urllib.request import Request
from bs4 import BeautifulSoup
from lxml import etree
import pymongo client = pymongo.MongoClient(host="127.0.0.1")
db = client.dianping #库名dianping
collection = db.classification #表名classification import redis #导入redis数据库
r = redis.Redis(host='127.0.0.1', port=6379, db=0) # client = pymongo.MongoClient(host="192.168.60.112")
# myip = client['myip'] # 给数据库命名
def secClassFind(selector, classid):
secItems = selector.xpath('//div[@class="sec-items"]/a')
for secItem in secItems:
url = secItem.get('href') #得到url
title = secItem.text
classid = collection.insert({'classname': title, 'pid': classid})
classurl = '%s,%s' % (classid, url) #拼串
r.lpush('classurl', classurl) #入库 def Public(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} #协议头
req_timeout = 5
req = Request(url=url, headers=headers)
f = urlopen(req, None, req_timeout)
s = f.read()
s = s.decode("utf-8")
# beautifulsoup提取
soup = BeautifulSoup(s, 'html.parser')
links = soup.find_all(name='li', class_="first-item")
for link in links:
selector = etree.HTML(str(link))
# indexTitleUrls = selector.xpath('//a[@class="index-title"]/@href')
# # 获取一级类别url和title
# for titleurl in indexTitleUrls:
# print(titleurl)
indexTitles = selector.xpath('//a[@class="index-title"]/text()')
for title in indexTitles:
# 第二级url
print(title)
classid = collection.insert({'classname': title, 'pid': None})
secClassFind(selector, classid)
print('---------')
# secItems = selector.xpath('//div[@class="sec-items"]/a')
# for secItem in secItems:
# print(secItem.get('href'))
# print(secItem.text)
print('-----------------------------')
#
# myip.collection.insert({'name':secItem.text})
# r.lpush('mylist', secItem.get('href')) # collection.find_one({'_id': ObjectId('5a14c8916d123842bcea5835')}) # connection = pymongo.MongoClient(host="192.168.60.112") # 连接MongDB数据库 # post_info = connection.myip # 指定数据库名称(yande_test),没有则创建
# post_sub = post_info.test # 获取集合名:test
Public('http://www.dianping.com/')

  

python爬取大众点评并写入mongodb数据库和redis数据库的更多相关文章

  1. python爬取大众点评

    拖了好久的代码 1.首先进入页面确定自己要抓取的数据(我们要抓取的是左侧分类栏-----包括美食.火锅)先爬取第一级分类(美食.婚纱摄影.电影),之后根据第一级链接爬取第二层(火锅).要注意第二级的p ...

  2. Python 爬取大众点评 50 页数据,最好吃的成都火锅竟是它!

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 胡萝卜酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  3. 用Python爬取大众点评数据,推荐火锅店里最受欢迎的食品

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:有趣的Python PS:如有需要Python学习资料的小伙伴可以加点 ...

  4. python爬虫实战---爬取大众点评评论

    python爬虫实战—爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经 ...

  5. python爬虫爬取大众点评并导入redis

    直接上代码,导入redis的中文编码没有解决,日后解决了会第一时间上代码!新手上路,多多包涵! # -*- coding: utf-8 -*- import re import requests fr ...

  6. python 爬取段子网段子写入文件

    import requests import re 进入网址 for i in range(1,5): page_url = requests.get(f"http://duanziwang ...

  7. 利用Python爬取豆瓣电影

    目标:使用Python爬取豆瓣电影并保存MongoDB数据库中 我们先来看一下通过浏览器的方式来筛选某些特定的电影: 我们把URL来复制出来分析分析: https://movie.douban.com ...

  8. Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)

    1.  爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...

  9. Python:将爬取的网页数据写入Excel文件中

    Python:将爬取的网页数据写入Excel文件中 通过网络爬虫爬取信息后,我们一般是将内容存入txt文件或者数据库中,也可以写入Excel文件中,这里介绍关于使用Excel文件保存爬取到的网页数据的 ...

随机推荐

  1. viewer.js的简单练习

    html <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8& ...

  2. 利用vertical-align:middle垂直居中

    以前总是以为vertical-align与text-align是同样的道理,一个是垂直居中,一个是水平居中,结果在这里一点效果也没有.事实上vertical-align与text-align完全不一样 ...

  3. PHP随手记1--内置函数date

    1. date_default_timezone_set date_default_timezone_set — 设定用于一个脚本中所有日期时间函数的默认时区 在php中只用 mktime() 函数时 ...

  4. 使用python将多个excel合并

    最近看视频学习,老师布置了个作业,关于如何使用python将多个excel进行合并,老师写的代码我感觉比较复杂,下面是我自己改良之后较简单的方式. 实现这个功能主要有两种方法,一种是用xlwd,xls ...

  5. 乌龟棋dp

    传送门题目:https://www.luogu.org/problem/show?pid=1541 其实这道题想到了就很简单,但很难想到用思维的dp,这非常少见. 看到每张牌不超过40张,这数据范围就 ...

  6. git操作之上传gitthub

    push 失败解决方法: 分支操作: 分支操作之覆盖: 主master操作:

  7. Web Api 基于Zookeeper的服务注册与发现

    安装与差异 Zookeeper安装请参考我上篇文章 http://www.cnblogs.com/woxpp/p/7700368.html 基于Nginx的服务提供和消费 基于zookeeper的服务 ...

  8. 剑指Offer_12_矩阵中的路径(参考问题:马踏棋盘)

    题目描述  请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...

  9. HDU_1698 Just a Hook(线段树+lazy标记)

    pid=1698">题目请点我 题解: 接触到的第一到区间更新,须要用到lazy标记.典型的区间着色问题. lazy标记详情请參考博客:http://ju.outofmemory.cn ...

  10. mysql数据表最高速迁移,mysql的存储引擎为:myisam

    本文链接:http://blog.csdn.net/u010670689/article/details/41346689 需求: 开发产品过程中,有个项目分支,数据库须要带数据拷贝,可是表的数据非常 ...