FlyingAtom FlyingAtom - 9 days ago 6
Python Question

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'))

Answer

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")