五.BeautifulSoup大众点评爬取店铺信息,存储到mongodb

#coding:utf-8
from pymongo import MongoClient
import requests,re
from multiprocessing import Pool
from bs4 import BeautifulSoup
import pymongo
from matplotlib import pyplot as plt
#开启数据库
cn=MongoClient('localhost',27017)
db=cn.job
table=db.dzdp801
#初始化数据库
table.remove({})

def geturl(i):
    print u"正在抓取第%d页"%i
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    url='http://www.dianping.com/search/category/1/10/r801p{}'.format(i)
    rq=requests.get(url,headers=headers)
    bs=BeautifulSoup(rq.content,'lxml')
    div=bs.find_all('div',class_='txt')
    for mode in div:
        quyu=''
        dic_md={}
        dic_md['_id']=mode.find('div',class_='tit').a['title']
        dic_md['level']=mode.find('div',class_='comment').span['title']
        try:
            dic_md['dingping']=mode.find('a',class_='review-num').b.string
        except:
            pass
        try:
            dic_md['comment_url']='http://www.dianping.com/'+mode.find('a',class_='review-num')['href']
        except:
            pass
        if mode.find('a',class_='mean-price').b:
            dic_md['money']=mode.find('a',class_='mean-price').b.string
        if mode.find('span',class_='tag'):
            dic_md['type_']=mode.find('span',class_='tag').string
        if mode.find(attrs={'data-midas-extends':"module=5_ad_kwregion"}):
            quyu=mode.find(attrs={'data-midas-extends':"module=5_ad_kwregion"}).string
        dic_md['place']=quyu+' '+mode.find('span',class_='addr').string
        pingfen=mode.find('span',class_='comment-list')
        try:
            if re.findall('\d{1,}\.\d',pingfen.get_text()):
                list_data=re.findall('\d{1,}\.\d',pingfen.get_text())
                dic_md['kouwei']=list_data[0]
                dic_md['huanjing']=list_data[1]
                dic_md['fuwu']=list_data[2]
        except:
            pass
        table.save(dic_md)
def deal_data():
    type_=[]
    for i in table.find():
        type_.append(i['type_'])
    new_dic=dict([(i, type_.count(i)) for i in type_])
 #   sorted_new_dic=sorted(new_dic.items(), key=operator.itemgetter(1))
    #画饼型图
    plt.figure()
    places=new_dic.keys()
    sizes=new_dic.values()
    plt.pie(sizes,explode=None,labels=places,colors=None,labeldistance=1.05,autopct='%3.1f%%',shadow=False,startangle=90,pctdistance=0.8)
    plt.axis('equal')
    plt.legend()
    plt.show()

def fuwu():
    type_=[]
    n1=n2=n3=n4=n5=n6=n7=n8=n9=n10=0
    s1=s2=s3=s4=s5=s6=s7=s8=s9=s10=0
    for i in table.find():
        type_.append(i['type_'])
    for i in table.find({'fuwu':{'$exists':True}}):
        if int(i['dingping'])>100:
            if i['type_']==u'小吃快餐':
                n1+=1
                s1=s1+float(i['fuwu'])
            if i['type_']==u'面包甜点':
                n2+=1
                s2=s2+float(i['fuwu'])
            if i['type_']==u'西餐':
                n3+=1
                s3=s3+float(i['fuwu'])
            if i['type_']==u'川菜':
                n4+=1
                s4=s4+float(i['fuwu'])
            if i['type_']==u'本帮江浙菜':
                n5+=1
                s5=s5+float(i['fuwu'])
            if i['type_']==u'咖啡厅':
                n6+=1
                s6=s6+float(i['fuwu'])
            if i['type_']==u'日本菜':
                n7+=1
                s7=s7+float(i['fuwu'])
            if i['type_']==u'火锅':
                n8+=1
                s8=s8+float(i['fuwu'])
    list_type=[u'小吃快餐',u'面包甜点',u'西餐',u'川菜',u'本帮江浙菜',u'咖啡厅',u'日本菜',u'火锅']
    print n1,n2,n3,n4,n5,n6,n7,n8
    data_type=[round(s1/n1,2),round(s2/n2,2),round(s3/n3,2),round(s4/n4,2),round(s5/n5,2),round(s6/n6,2),round(s7/n7,2),round(s8/n8,2)]
    plt.xlabel(u"餐厅类型")
    plt.ylabel(u"评分")
    plt.bar(range(len(data_type)), data_type, tick_label=list_type)
    plt.show()


if __name__=="__main__":
  #  fuwu()
    pool=Pool(processes=5)
    pool.map_async(geturl,range(1,51))
    pool.close()
    pool.join()
    deal_data()



好的,我可以给你提供一个大致的思路: 1. 安装selenium和pymongo库 ``` pip install selenium pymongo ``` 2. 下载Chrome浏览器和相应版本的chromedriver,并配置环境变量 3. 使用selenium模拟浏览器打开豆瓣读书网站,并搜索需要的书籍,获取搜索结果页面的源代码 ```python from selenium import webdriver # 打开浏览器 driver = webdriver.Chrome() # 打开网页 url = "https://book.douban.com/" driver.get(url) # 搜索书籍 keyword = "Python" search_box = driver.find_element_by_xpath("//input[@name='search_text']") search_box.send_keys(keyword) search_box.submit() # 获取搜索结果页面的源代码 page_source = driver.page_source # 关闭浏览器 driver.quit() ``` 4. 使用BeautifulSoup解析页面源代码,获取需要的数据 ```python from bs4 import BeautifulSoup # 解析页面源代码 soup = BeautifulSoup(page_source, "html.parser") # 获取书籍列表 book_list = soup.find_all("li", class_="subject-item") # 遍历书籍列表,获取书籍信息 for book in book_list: # 获取书名 title = book.find("h2").a["title"] # 获取评分 rating = book.find("span", class_="rating_nums").get_text() # 获取出版信息 pub_info = book.find("div", class_="pub").get_text().strip() # 存储数据到数据库 db.books.insert_one({"title": title, "rating": rating, "pub_info": pub_info}) ``` 5. 将数据存储MongoDB ```python from pymongo import MongoClient # 连接MongoDB数据库 client = MongoClient("mongodb://localhost:27017/") db = client["douban"] # 存储数据到数据库 for book in book_list: # 获取书籍信息 ... # 存储数据到数据库 db.books.insert_one({"title": title, "rating": rating, "pub_info": pub_info}) ``` 以上是一个简单的爬虫程序,可以根据自己的需求进行修改和优化。另外需要注意的是,爬取网站数据需要遵守网站的相关规定和法律法规,不得进行恶意爬取和商业利用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值