Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:14:00+00:00 2026-06-14T23:14:00+00:00

I have this piece of code which uses BeatifulSoup to scrape content from a

  • 0

I have this piece of code which uses BeatifulSoup to scrape content from a site.

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)

An HTML example input:

<div class="short-description">
                                        <div class="std">Barre chocolat au lait fourrée à la crème de lait<br>25g, bio et équitable<br>Produit bio contrôlé par Bio Inspecta</div>
                </div>

<div class="box-collateral box-description">
    <h2>Description du produit</h2>
    <div class="std">
        <div><strong>CHOKICHOC : la barre de chocolat au lait, fourrée à la crème de lait</strong></div><br>
<div style="text-align: justify;">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></div><br>
<div>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.</div>    </div>
</div>
<div class="product_shop">
<p>

                <label class="garanties_adm">
                <strong>Les garanties :</strong></label>
                <img src="/images/adm/icon-garantie-fairtrade-ab.png">
                            </p>
<p>
                                <label><strong>Origine :</strong></label> 
                Burkina Faso, Cercle des Sécheurs                           </p>
<p> </p>

</div>

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&eacute;e &agrave; la cr&egrave;me de
lait</strong> CHOKICHOC : la barre de chocolat au lait, fourr&eacute;e
&agrave; la cr&egrave;me de lait  Exquis m&eacute;lange des plus fins
cacaos et de l&rsquo;aromatique sucre bio du Paraguay, CHOKICHOC est
compos&eacute;e exclusivement de mati&egrave;res premi&egrave;res
cultiv&eacute;es sans additif ni ar&ocirc;me artificiel. Tous les
ingr&eacute;dients proviennent de cultures biologiques.
<strong>L&eacute;g&egrave;re, fondante, id&eacute;ale pour le
go&ucirc;ter, un vrai d&eacute;lice!</strong> L&eacute;g&egrave;re,
fondante, id&eacute;ale pour le go&ucirc;ter, un vrai d&eacute;lice!
La commercialisation des barres CHOKICHOC garantit un prix minimum
pour le producteur, des contrats d&rsquo;achats &agrave; long terme
ainsi que le pr&eacute;financement partiel de la r&eacute;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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:14:01+00:00Added an answer on June 14, 2026 at 11:14 pm

    the only way i can get this to work is to set the encoding like:

    # we get the country and producer
    for txt in product_shop.findAll(text=True):
       get_origin = re.search('Origine',txt,re.I)
       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.encode("iso-8859-1"))
               adm_product.append(producer.encode("iso-8859-1"))
    

    this then returns the ‘list’ like:

    .... 'Burkina Faso', 'Cercle des S\xe9cheurs']
    

    item[1] is correctly added to the list as i was using chrome to inspect the element, which correctly changed the html to:

    <div style="text-align: justify;">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></div>
    

    where as if i look at the source code, it is like:

    <div style="text-align: justify;">Exquis m&eacute;lange des plus fins cacaos et de l&rsquo;aromatique sucre bio du Paraguay, CHOKICHOC est compos&eacute;e exclusivement de mati&egrave;res premi&egrave;res cultiv&eacute;es sans additif ni ar&ocirc;me artificiel. Tous les ingr&eacute;dients proviennent de cultures biologiques. <strong>L&eacute;g&egrave;re, fondante, id&eacute;ale pour le go&ucirc;ter, un vrai d&eacute;lice!</strong></div><br />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which check the last customer_id from the db.
Ok i have this piece of code from which i took from W3schools :-
I have this piece of code which I'm hoping will be able to tell
Possible Duplicate: php loop through associative arrays i have this piece of code which
I am somewhat new to R, and i have this piece of code which
I have this piece of Open MP code here which performs an integeration of
I have this piece of code: if(($op == q)); then which throws out this
I have the following piece of code which uses the ALAssets library to store
I have found the following piece of code which uses XmlHttpRequest to read JSON
I have this piece of code, which copies a plist file to the ApplicationSupport

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.