1

Below is my code: I am trying to turn the loop results I get from this code into a list. Any ideas?

import requests
from bs4 import BeautifulSoup

page = requests.get('http://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168')

soup = BeautifulSoup(page.text, 'html.parser')

for x in soup.find_all(class_='tombstone-container'):
    y = (x.get_text())
    print (y)
1
  • 1
    Any ideas? - Work your way through the Tutorial practicing the examples given and reading the Standard Library documentation during that process. This will give you a general knowledge of the tools Python offers to help you solve your problem(s) and will probably help you get ideas how to proceed. Commented Sep 28, 2017 at 15:00

3 Answers 3

4

A straightforward way to convert the results of a for loop into a list is list comprehension.

We can convert:

for x in soup.find_all(class_='tombstone-container'):
    y = (x.get_text())
    print (y)

into:

result = [x.get_text() for x in soup.find_all(class_='tombstone-container')]

Basic (list comprehension has a more advanced syntax) has as grammar:

[<expr> for <var> in <iterable>]

it constructs a list where Python will iterate over the <iterable> and assigns values to <var> it adds for every <var> in <iterable> the outcome of <expr> to the list.

Sign up to request clarification or add additional context in comments.

1 Comment

It is also possible to chain more for..in s
3

if you don't want to change much of your code, create an empty list before your loop like this

myList = []

and in your loop append the content like this:

myList.append(str(x.get_text())) # EDIT2

EDIT1: The reason I used myList.append(...) above instead of myList[len(myList)] or something similar, is because you have to use the append method to extend already existing lists with new content.

EDIT2: Concerning your problem with None pointers in your list: If your list looks like [None, None, ...] when printed after the for loop, you can be sure now that you have still a list of strings, and they contain the word None (like this ['None','None',...]). This would mean, that your x.get_text() method returned no string, but a None-pointer from the beginning. In other words your error would lie buried somewhere else.

Just in case. A complete example would be:

myList = []
for x in soup.find_all(class_='tombstone-container'):
    # do stuff, but make sure the content of x isn't modified
    myList.append(str(x.get_text()))
    # do stuff

Comments

2

Just loop over it.

map(lambda x: x.get_text(), soup.find_all(class_='tombstone-container'))

2 Comments

If the code runs on Python 3 the result won't be a list.
@Matthias yeah, sadly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.