使用Bs4 Python复制页面并删除多个空格

使用Bs4 Python复制页面并删除多个空格,python,beautifulsoup,Python,Beautifulsoup,我尝试刮取页面的某些部分: 要查找元素描述,您可以进入如下页面: 现在我需要得到部分英文描述 因此,我尝试通过td在选定行中获取元素,如下所示: import re import requests from bs4 import BeautifulSoup url = 'https://tuning-tec.com/mercedes_w164_ml_mklasa_0507_black_led_seq_lpmed0-5789i' ajax_url = 'https://tuning-tec.

我尝试刮取页面的某些部分:

要查找元素描述,您可以进入如下页面:

现在我需要得到部分英文描述

因此,我尝试通过td在选定行中获取元素,如下所示:

import re
import requests
from bs4 import BeautifulSoup

url = 'https://tuning-tec.com/mercedes_w164_ml_mklasa_0507_black_led_seq_lpmed0-5789i'
ajax_url = 'https://tuning-tec.com/_template/_show_normal/_show_charlong.php?itemId={}'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

item_id = re.findall(r"ajax_update_stat\('(\d+)'\)", soup.text)[0]
soup2 = BeautifulSoup(requests.get(ajax_url.format(item_id)).content, 'html.parser')

def unwrapElements(soup, elementsToFind):
    elements = soup.find_all(elementsToFind)
    for element in elements:
        element.unwrap()

#Get content what i need (find tr and  get seconnd  td where is english description)
description=soup2.findAll('tr')[1].findAll('td')[1]
description.append(soup2.new_tag('br'))
description.append((soup2.findAll('tr')[2].findAll('td')[1]))
description.append(soup2.findAll('tr')[3].findAll('td')[1])
description.append(soup2.new_tag('br'))
description.append(soup2.findAll('tr')[4].findAll('td')[1])

#Remove tags what I dont wont
unwrapElements(description, "font")
unwrapElements(description, "span")
unwrapElements(description, "strong")
unwrapElements(description, "b")
unwrapElements(description, "td")

print(description)
我是如此接近获得良好的输出,我添加了一些“BR”来打破新的行,但我看到多个空格之间

<td>Description<br/>Projector  
                                                     headlights with LED Parking 
                        light.<br/>With Dynamic Turn       
      Signal.<br/>100%       brand             new, come in       pair 
      (left &amp; right).<br/>Approval:                      
      E-marked E13 -        approved.    Details<br/>Parking    
                     light: LED<br/>Blinker: LED
<br/>Low 
                              beam: H9   included <br/>High beam:           
                 H1   included <br/>Regulation:        electrical (with build in    
                     electrical adjuster).</td>
我怎么能修好它

第二个问题


当我打开标签时,为什么输出中有td?

类型
说明
bs4.element.Tag
。您可以使用
description.text
获取文本(这将去掉
标记)

格式有点奇怪,但我使用正则表达式删除了额外的空格——所有两边有空格的新行都被一个空格字符替换。然后我去掉了多余的空间

print(' '.join((re.sub(' *\n *',' ', description.text)).split()))

> DescriptionProjector headlights with LED Parking light.With Dynamic Turn Signal.100% brand new, come in pair (left & right).Approval: E-marked E13 - approved. DetailsParking light: LEDBlinker: LED Low beam: H9 included High beam: H1 included Regulation: electrical (with build in electrical adjuster).
谢谢你的帮助

我只是稍微改变一下

soup_string = str(description)

# print(soup_string)
print(' '.join((re.sub(' *\n *',' ', soup_string)).split()))
因为我需要“

”标记来断开新行。text“删除所有标记”

但是你的回答对我很有用。 非常感谢

soup_string = str(description)

# print(soup_string)
print(' '.join((re.sub(' *\n *',' ', soup_string)).split()))