鱼C论坛

 找回密码
 立即注册
查看: 591|回复: 1

运用mongodb数据存爬虫数据,为什么数据库中没有?

[复制链接]
发表于 2018-8-14 21:04:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import time
import re
import pymongo
from lxml import etree
import requests

client=pymongo.MongoClient('localhost',27017)
mydb=client['mydb']
musictop=mydb['muisctop']

headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}

def get_url_muisc(url):
    html=requests.get(url,headers=headers)
    selector=etree.HTML(html.text)
    muisc_hrefs=selector.xpath('//div[@class="p12"]/@href')
    for muisc_href in muisc_hrefs:
        get_muisc_info(muisc_href)

def get_muisc_info(url):
    html=requests.get(url,headers=headers)
    selector=etree.HTML(html.text)
    name=selector.xpath('//*[@id="wrapper"]/h1/span/text()')[0]
    author=selector.xpath('//*[@id="info"]/span[2]/text()')[0]
    styles=selector.xpath('//*[@id="info"]/span[3]/text()')[0]
    time=selector.xpath('//*[@id="info"]/span[5]/text()')[0]
    publishers=selector.xpath('//*[@id="info"]/span[6]/text()')[0]
    score=selector.xpath('//*[@id="interest_sectl"]/div/div[2]/strong/text')[0]
    print(name,author,time,publishers,score)
    info={
        'name':name,
        'author':author,
        'styles':styles,
        'time':time,
        'publishers':publishers,
        'score':score
    }
    musictop.insert_one(info)
if __name__=='__main__':
    urls=['https://music.douban.com/top250?start={}'.format(str(i)) for i in range(0,250,25)]
    for url in urls:
        get_url_muisc(url)
        time.sleep(2)

上面是准确的代码 为什么在robomongo看不到数据??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-7 00:18:25 | 显示全部楼层
你检查一下你的robomongo连接的mongodb的服务器配置的。db路径和你写入数据的路径是否一致。
还有,在代码中加入几个
  1. print(xxx)
复制代码
,检查你是否正确的获取到数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 16:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表