LGDDDDDD 2018-11-30 08:45 采纳率: 20%
浏览 708
已采纳

写了个简单的豆瓣音乐TOP250的爬虫,不知道为什么打印不出来也没有报错,求大神解答

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

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

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

def get_music_info(url):
html = requests.get(url, headers=headers)
selector = etree.HTML(html.text)
name=selector.xpath('//div[@id="wrapper"]/h1/span/text()')[0]
author=selector.xpath('//div[@id="info"]/span[1]/span/a/text()')[0]
styles=re.findall('流派: (.*?)
',html.text.re.S)
if len(styles)==0:
style='未知'
else:
style=styles[0].strip()

time=re.findall('<span class="pl">发行时间:</span>&nbsp;(.*?)<br />',html.text,re.S)[0].strip()

publishers=re.findall('出版者:.*?>;(.*?)</a>',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)

  • 写回答

1条回答 默认 最新

  • lyhsdy 2018-12-01 02:21
    关注

    这句错了

     music_hrefs=selector.xpath('//div[@class="p12"]/a/@href')
    

    正确是pl2 不是p12 不是1 不是1不是1是l

    下次提问使用插入代码粘贴代码

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条