Re: Need Help with the BeautifulSoup problem, please

2013-12-16 Thread 88888 Dihedral
On Monday, December 16, 2013 2:41:08 PM UTC+8, seas...@gmail.com wrote:
 I need to replace all tag b with span after ■. But the result from below 
 is '■   span style=REPLACED/span / font/font'
 
 Can you explain what I did wrong, please.
 
 
 
 s = '■bA/b bB/b bC/b bD/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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help with the BeautifulSoup problem, please

2013-12-16 Thread seaspeak
seas...@gmail.com於 2013年12月16日星期一UTC+8下午2時41分08秒寫道:
 I need to replace all tag b with span after ■. But the result from below 
 is '■   span style=REPLACED/span / font/font'
 
 Can you explain what I did wrong, please.
 
 
 
 s = '■bA/b bB/b bC/b bD/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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help with the BeautifulSoup problem, please

2013-12-16 Thread Andreas Perstinger

On 16.12.2013 07:41, seasp...@gmail.com wrote:

I need to replace all tag b with span after ■. But the result
frombelow is '■ span style=REPLACED/span / font/font'
Can you explain what I did wrong, please.

 s = '■bA/b bB/b bC/b bD/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
--
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help with the BeautifulSoup problem, please

2013-12-16 Thread seaspeak
8 Dihedral於 2013年12月16日星期一UTC+8下午4時02分42秒寫道:
 On Monday, December 16, 2013 2:41:08 PM UTC+8, seas...@gmail.com wrote:
 
  I need to replace all tag b with span after ■. But the result from 
  below is '■   span style=REPLACED/span / font/font'
 
  
 
  Can you explain what I did wrong, please.
 
  
 
  
 
  
 
  s = '■bA/b bB/b bC/b bD/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.

I think it's my fault, thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help with the BeautifulSoup problem, please

2013-12-16 Thread Peter Otten
seasp...@gmail.com wrote:

 I need to replace all tag b with span after ■. But the result from
 below is '■   span style=REPLACED/span / font/font'
 Can you explain what I did wrong, please.
 
 s = '■bA/b bB/b bC/b bD/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 = '■bA/b bB/b bC/b bD/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)


-- 
https://mail.python.org/mailman/listinfo/python-list


Need Help with the BeautifulSoup problem, please

2013-12-15 Thread seaspeak
I need to replace all tag b with span after ■. But the result from 
below is '■   span style=REPLACED/span / font/font'
Can you explain what I did wrong, please.

s = '■bA/b bB/b bC/b bD/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)
-- 
https://mail.python.org/mailman/listinfo/python-list