为什么程序没有报错,数据还是没有存入啊

qq_42473771 2018-06-17 02:51:01
import requests
from lxml import etree
import re
import pymongo
import time


client = pymongo.MongoClient('localhost',27017)
mydb = client['mydb']
musictop = mydb['musictop']
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36'
}

def get_url_music(url):
html = requsets.get(url,headers=headers)
selector = etree.HTML(html.text)
music_hrefs = selector.xpath('//a[@class="nbg"]/@href')
for music_href in music_hrefs:
get_music_info(music_href)

def get_music_href(url):
html = reequests.get(url,headers=headers)
selector = etree.HTML(html.text)
name = selector.xpath('//*[@id="wrapper"]/h1/span/text()')[0]
author = selector.xpath('//*[@id="info"]/span[1]/span/a/text()')[0]
styles = re.findall('<span class="pl">流派:</span>(.*?)< /br>',html.text,re.S)
if len(styles) == 0:
styles = '未知'
else:
style = styles[0].strip()
time = re.findall('发行时间:</span> (.*?)<br>',html.text,re.S)[0].strip()
publishers = re.findall('出版者:</span>" (.*?)"<br>',html.text,re.S)
if len(publishers) == 0:
publisher = '未知'
else:
publisher = publishers[0].strip()
score = selector.xpath('//*[@id="interest_sectl"]/div/div[2]/strong/text()')[0]
print(name,author,style,time,publisher,score)
info = {
'name':name,
'author':author,
'style':style,
'time':time,
'publisher':publisher,
'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_music(url)
time.sleep(2)
...全文
1257 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2018-06-17
  • 打赏
  • 举报
回复
没啥问题 自己print一些参数调试下

1,747

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧