JohnDotOwl
JohnDotOwl

Reputation: 3755

BeautifulSoup Loop not returned in Sequence

The Result returned is not in sequence , I need the result to be returned in sequence.

Trying to record Rankings.

def parse(self, response):
    sourceHtml = BeautifulSoup(response.body)
    soup = sourceHtml.find("dl", {"id": "resultList"})
    for link in soup.find_all('dd'):
        print(link.get('code'))

Upvotes: 1

Views: 51

Answers (1)

alecxe
alecxe

Reputation: 473863

If you want to have the printed "codes" in a list, just use a "list comprehension":

def parse(self, response):
    sourceHtml = BeautifulSoup(response.body)
    soup = sourceHtml.find("dl", {"id": "resultList"})
    return [link.get('code') for link in soup.find_all('dd')]

You can also improve the way you locate the elements and use a CSS selector:

def parse(self, response):
    soup = BeautifulSoup(response.body)
    return [link.get('code') for link in soup.select('dl#resultList dd')]

It is also a good idea to provide an underlying parser explicitly:

soup = BeautifulSoup(response.body, "html.parser")
# or soup = BeautifulSoup(response.body, "html5lib")
# or soup = BeautifulSoup(response.body, "lxml")

Upvotes: 1

Related Questions