qq_39687280 2019-12-14 19:10 采纳率: 0%
浏览 341
已结题

python bs4 find_all 有一行没有该属性 造成数据错位

想爬豆瓣读书的网页,在爬取评价星级时,某一条目没有这个层级的内容,所以使用BeautifulSoup的find_all功能时,数据错位了,怎么解决。可以用条件语句吗

titles=[]
authors=[]
ratings=[]
comments=[]

hds={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
source = requests.get('https://book.douban.com/tag/%E4%B8%AD%E5%9B%BD%E6%96%87%E5%AD%A6?start=280&type=R',headers=hds)
print(source.status_code)
print(source.text)
soup=bs.BeautifulSoup(source.content,'html.parser')

for t in soup.find_all('div',class_='info'):
        for title in t.find_all('h2',class_=""):
            print(title.text.strip())
            titles.append(title.text.strip().replace('\n',''))
for author in soup.find_all("div",class_="pub") :
        print(author.text.strip())
        authors.append(author.text.strip()) 

for rating in soup.find_all('div',class_='star clearfix'):
    if 
        print(rating.text)
        ratings.append(rating.text.strip())
for comment in soup.find_all('span',class_='pl'):
        print(comment.text.strip().replace('(','').replace('人评价)',''))
        comments.append(comment.text.strip().replace('(','').replace('人评价)',''))

图片说明图片说明

  • 写回答

1条回答

  • 放风喽 2019-12-15 21:43
    关注

    ratings.append(rating.text.strip())
    去掉空白???
    《祁连如梦》没有评分,是不是去掉空白的时候,直接去掉了??》

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现