Python string.replace()

 #coding=utf-8

import re
from bs4 import BeautifulSoup as BS
import requests
import hackhttp

# BeautifulSoup

url='https://www.douyu.com/directory/game/LOL'
r=requests.get(url,verify=False)
html=r.content

soup=BS(html,'lxml')
bbs=soup.find_all(name='h3',attrs={'class':'ellipsis'})
print bbs
for news in bbs:
print news.string.replace('\r','').replace('\n','')

结果:
Traceback (most recent call last):
File "spider.py", line 18, in <module>
print news.string.replace('\r','').replace('\n','')
**AttributeError: 'NoneType' object has no attribute 'replace'**

先看看news.string是不是为None,没有获取到对应的数据。

你这个最后的代码是想实现什么功能呀

news里面前提取的内容是None。应该是你提取,

标签时候把一些没用的标签也提取出来了。 去掉这个就可以了
for news in bbs:
if(news.string!=None):
print( news.string.replace('\r','').replace('\n',''))