我想要刮和html页面,然后被<br>标记阻塞。我尝试根据<br>作为分隔符来分割html内容。
from urllib.request import urlopen
import re
from bs4 import BeautifulSoup
url = 'https://www.ouedkniss.com/telephones'
html = urlopen(url)
bs = BeautifulSoup(html, 'html.parser')
text_tag = bs.find('span', class_="annonce_get_description",
itemprop="description")
words = text_tag.text.split('<br/>')
print(words)正如你在结果中看到的那样,当我拆分文本时,什么也不会发生,它会在一起?
.étéléphonesémoire: 128 GO Produit中性版苹果公司iphone 6s 16 GO avec收费原\r\n.kitmains,blanc.gold .état中性版免费版:33000 GO\r\n▶️iphone 6s 32 GO avec原\r\n.kitmains,blanc.gold .état中性版:35000 GO\r\n▶️iphone 6s 128 GO avec原\r\n.kitmains,blanc.gold .ét‘
发布于 2019-08-07 10:50:34
B在获得.text时删除所有标记,这样就没有要拆分的<br>了。
您可以尝试.get_text(separator=...)来获得它,并且它应该在来自不同标记的文本之间添加分隔符。它应该使用separator而不是<br>。然后你可以使用split(separator)
words = text_tag.get_text(separator='|', strip=True).split('|')如果文本中使用'|',则使用更多的唯一分隔符。
words = text_tag.get_text(separator='|br|', strip=True).split('|br|')但它可能会使separator取代其他标签,如'Mémoire : <b>64 GO</b>'中的<b>。
您可以在原始<br/>中用separator替换所有的split(separator),然后使用split(separator)
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = 'https://www.ouedkniss.com/telephones'
html = urlopen(url)
html = html.read()
html = html.replace(b'<br/>', b'|br|')
bs = BeautifulSoup(html, 'html.parser')
text_tag = bs.find('span', class_="annonce_get_description",
itemprop="description")
words = text_tag.text.split('|br|')
print(words)您只能使用内部HTML来完成它。
'<br/>'替换为separator,find())split(separator)代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = 'https://www.ouedkniss.com/telephones'
html = urlopen(url)
bs = BeautifulSoup(html, 'html.parser')
text_tag = bs.find('span', class_="annonce_get_description",
itemprop="description")
inner_html = text_tag.encode_contents()
inner_html = inner_html.replace(b'<br/>', b'|br|')
bs = BeautifulSoup(inner_html, 'html.parser')
words = bs.text.split('|br|')
print(words)发布于 2019-08-07 10:44:57
<br/>是html标记,当您使用text_tag.text时,只获取文本,而不是带有标记的html部分。
如果你想获取这些信息,你可以探索更多:
print(text_tag.contents)
# output:
# ['Smartphones',
# <br/>,
# <b>Double puces</b>,
# <br/>,
# 'Mémoire : 128 GO ',
# <br/>,
# 'Bluetooth Wifi ',
# <b>4G</b>,
# ' ',
# <br/>,
# 'Ecran 5.99 pouces ',
# <br/>,
# 'Etat neuf / Sous emballage ',
# <br/>,
# <span class="annonce_description_preview ">Le smartphone et comme neuf utilisé pour quelque heures. fourni avec incassables original ! merci </span>]你也可以尝试:
print(''.join(str(e) for e in text_tag.contents).split('<br/>'))
#output:
# ['Smartphones',
# '<b>Double puces</b>',
# 'Mémoire : 128 GO ',
# 'Bluetooth Wifi <b>4G</b> ',
# 'Ecran 5.99 pouces ',
# 'Etat neuf / Sous emballage ',
# '<span class="annonce_description_preview ">Le smartphone et comme neuf utilisé pour quelque heures. fourni avec incassables original ! merci </span>']或者如果你想要一个更好的方式:
content = ['']
for item in text_tag.contents:
if hasattr(item, 'text'):
text = item.text
else:
text = str(item)
if '<br/>' in str(item):
content.append(text.strip())
else:
content[-1] = f'{content[-1]} {text.strip()}'.strip()
print(content)
# output
# ['Smartphones',
# 'Double puces',
# 'Mémoire : 128 GO',
# 'Bluetooth Wifi 4G',
# 'Ecran 5.99 pouces',
# 'Etat neuf / Sous emballage',
# 'Le smartphone et comme neuf utilisé pour quelque heures. fourni avec incassables original ! merci']https://stackoverflow.com/questions/57392407
复制相似问题