【问题标题】:I keep getting some gaps in my text after scraping刮擦后,我的文本中不断出现一些空白
【发布时间】:2017-01-20 14:07:38
【问题描述】:

任何人都知道如何消除抓取文本中的这些空白,因为这使得将它们存储在 csv 文件中变得非常困难。 html中的“用户名”类有很多空白,如下图所示。

for i in range(0,2):
    link = (df.link.iloc[i])
    source1 = urllib.request.urlopen(link).read()
    soup1 = bs.BeautifulSoup(source1,'lxml')
    for username in soup1.find_all('div', class_="user-name"):
        print (username.get_text())
#    for time in soup1.find_all('div',class_="thread-ago"):
        
example = open('generalinfo.csv','w')
wr = csv.writer(example,quoting = csv.QUOTE_ALL)        
wr.writerows(lists)    
example.close()  

【问题讨论】:

    标签: python csv export-to-csv


    【解决方案1】:

    使用

    username.get_text().strip()
    

    去除空白。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2016-02-04
      • 2022-01-02
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      相关资源
      最近更新 更多