[Tutor] correctly encoding of BeautifulSoup content

Norman Khine norman at khine.net
Mon Nov 12 16:48:36 CET 2012


hello,

i have this piece of code (http://pastie.org/5366200) which uses
BeatifulSoup to scrape content from a site, the html for the example
can be seen here http://pastie.org/5366172

               short_description = soup.find('div', attrs={"class":
"short-description"})
               if short_description:
                   short_desc = short_description.find('div',
attrs={"class": "std"})
                   if short_desc:
                       adm_product.append(short_desc.renderContents())

               long_description = soup.find('div', attrs={"class":
"box-collateral box-description"})
               if long_description:
                   long_desc = long_description.find('div',
attrs={"class": "std"})
                   if long_desc:
                       adm_product.append(long_desc.renderContents())
                       L = []
                       for tag in long_desc.recursiveChildGenerator():
                           if isinstance(tag,BeautifulSoup.Tag):
                               L.append(tag.renderContents())
                       desc = " ".join(v for v in L if v > 0)
                       print desc
                       adm_product.append(desc)
                   else:
                       adm_product.append('pas du description')

               # we get the country and producer
               for txt in product_shop.findAll(text=True):
                   if re.search('Origine',txt,re.I):
                       origin = txt.next.strip()
                       try:
                           country, producer = origin.split(', ')
                       except Exception, e:
                           pass
                       else:
                           adm_product.append(country)
                           adm_product.append(producer)

when i print the adm_product list i get:

['002267', 'Barre chocolat au lait fourr\xc3\xa9e \xc3\xa0 la
cr\xc3\xa8me de lait<br />25g, bio et \xc3\xa9quitable<br />Produit
bio contr\xc3\xb4l\xc3\xa9 par Bio Inspecta', '<strong>CHOKICHOC : la
barre de chocolat au lait, fourrée à la crème de
lait</strong> CHOKICHOC : la barre de chocolat au lait, fourrée
à la crème de lait  Exquis mélange des plus fins
cacaos et de l’aromatique sucre bio du Paraguay, CHOKICHOC est
composée exclusivement de matières premières
cultivées sans additif ni arôme artificiel. Tous les
ingrédients proviennent de cultures biologiques.
<strong>Légère, fondante, idéale pour le
goûter, un vrai délice!</strong> Légère,
fondante, idéale pour le goûter, un vrai délice!
La commercialisation des barres CHOKICHOC garantit un prix minimum
pour le producteur, des contrats d’achats à long terme
ainsi que le préfinancement partiel de la récolte.',
'0,90\xc2\xa0',
u'/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/0/0/002267_2.jpg',
u'Burkina Faso', u'Cercle des S\xe9cheurs']

my list item[1] is correctly encoded, but item[2] is not; nor are the
last 2 items

what am i missing?

thanks


More information about the Tutor mailing list