Need Help with the BeautifulSoup problem, please

S

seaspeak

I need to replace all tag <b> with <span> after â– . But the result from below is 'â–  <span style="REPLACE">D</span> / <font></font>'
Can you explain what I did wrong, please.

s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
soup = BeautifulSoup(s)
for i in soup.find_all(text='â– '):
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
break
else:
if ii.name=='b':
tag.string=ii.string
print(ii.replace_with(tag))
print(soup)
 
8

88888 Dihedral

I need to replace all tag <b> with <span> after â– . But the resultfrom below is 'â–  <span style="REPLACE">D</span> / <font></font>'

Can you explain what I did wrong, please.



s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'

soup = BeautifulSoup(s)

for i in soup.find_all(text='â– '):

tag = soup.new_tag('span')

tag['style'] = 'REPLACE'

for ii in i.find_next_siblings():

if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':

break

else:

if ii.name=='b':

tag.string=ii.string

print(ii.replace_with(tag))

print(soup)

I think you should try some descent
free editors such as notepad++
for your source codes to
replace trivial strings without
programmig.
 
S

seaspeak

(e-mail address removed)æ–¼ 2013å¹´12月16日星期一UTC+8下åˆ2時41分08秒寫é“:
I need to replace all tag <b> with <span> after â– . But the resultfrom below is 'â–  <span style="REPLACE">D</span> / <font></font>'

Can you explain what I did wrong, please.



s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'

soup = BeautifulSoup(s)

for i in soup.find_all(text='â– '):

tag = soup.new_tag('span')

tag['style'] = 'REPLACE'

for ii in i.find_next_siblings():

if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':

break

else:

if ii.name=='b':

tag.string=ii.string

print(ii.replace_with(tag))

print(soup)

the point is the result seems wrong. I don't know if that is my problem.
I simplify the code to emphasize the problem, there's no way an editor can do what I wanna do.
 
S

seaspeak

88888 Dihedralæ–¼ 2013å¹´12月16日星期一UTC+8下åˆ4時02分42秒寫é“:
I need to replace all tag <b> with <span> after â– . But the result from below is 'â–  <span style="REPLACE">D</span> / <font></font>'
Can you explain what I did wrong, please.
s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
soup = BeautifulSoup(s)
for i in soup.find_all(text='â– '):
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':

if ii.name=='b':
tag.string=ii.string
print(ii.replace_with(tag))

print(soup)



I think you should try some descent

free editors such as notepad++

for your source codes to

replace trivial strings without

programmig.

I think it's my fault, thanks
 
A

Andreas Perstinger

I need to replace all tag <b> with <span> after â– . But the result
frombelow is 'â–  <span style="REPLACE">D</span> / <font></font>'
Can you explain what I did wrong, please.

s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
soup = BeautifulSoup(s)
for i in soup.find_all(text='â– '):
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
break
else:
if ii.name=='b':
tag.string=ii.string
print(ii.replace_with(tag))
print(soup)

You are only creating one new tag but as I understand your problem you
want to replace each b-element with a new tag. Simply move the tag
creating part:

for i in soup.find_all(text='â– '):
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
break
else:
if ii.name=='b':
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
tag.string=ii.string
print(ii.replace_with(tag))

And please read
https://wiki.python.org/moin/GoogleGroupsPython
if you want to continue using Google Groups for accessing this list.

Bye, Andreas
 
P

Peter Otten

I need to replace all tag <b> with <span> after â– . But the result from
below is 'â–  <span style="REPLACE">D</span> / <font></font>'
Can you explain what I did wrong, please.

s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
soup = BeautifulSoup(s)
for i in soup.find_all(text='â– '):
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
break
else:
if ii.name=='b':
tag.string=ii.string
print(ii.replace_with(tag))
print(soup)

It looks like you cannot reuse a tag. Try

s = 'â– <b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
soup = BeautifulSoup(s)
for i in soup.find_all(text='â– '):
for ii in i.find_next_siblings():
if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
break
else:
if ii.name=='b':
tag = soup.new_tag('span')
tag['style'] = 'REPLACE'
tag.string=ii.string
print(ii.replace_with(tag))
print(soup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top