Python - converting to list
<pre><code>import requests
from bs4 import BeautifulSoup 

webpage = requests.get("http://www.nytimes.com/")
soup = BeautifulSoup(requests.get("http://www.nytimes.com/").text, "html.parser")
for story_heading in soup.find_all(class_="story-heading"): 
articles = story_heading.text.replace('\n', '').replace('  ', '')
print (articles)
</code></pre>

<p>There is my code, it prints out a list of all the article titles on the website. I get strings: </p>

<blockquote>
  <p>Looking Back: 1980 | Funny, but Not Fit to Print</p>
  
  <p>Brooklyn Studio With Room for Family and a Dog</p>
  
  <p>Search for Homes for Sale or Rent</p>
  
  <p>Sell Your Home</p>
</blockquote>

<p>So, I want to convert this to a list = ['Search for Homes for Sale or Rent', 'Sell Your Home', ...], witch will allow me to make some other manipulations like random.choice etc.<br>
I tried:</p>

<pre><code>alist = articles.split("\n")
print (alist)
</code></pre>

<blockquote>
  <p>['Looking Back: 1980 | Funny, but Not Fit to Print'] </p>
  
  <p>['Brooklyn Studio With Room for Family and a Dog']</p>
  
  <p>['Search for Homes for Sale or Rent']</p>
  
  <p>['Sell Your Home']</p>
</blockquote>

<p>It is not a list that I need. I'm stuck. Can you please help me with this part of code. </p>

<p>You are constantly overwriting <code>articles</code> with the next value in your list. What you want to do instead is make <code>articles</code> a list, and just <code>append</code> in each iteration: </p>

<pre><code>import requests
from bs4 import BeautifulSoup 

webpage = requests.get("http://www.nytimes.com/")
soup = BeautifulSoup(requests.get("http://www.nytimes.com/").text, "html.parser")
articles = []
for story_heading in soup.find_all(class_="story-heading"): 
    articles.append(story_heading.text.replace('\n', '').replace('  ', ''))
print (articles)
</code></pre>

<p>The output is huge, so this is a small sample of what it looks like:</p>

<pre><code>['Global Deal Reached to Curb Chemical That Warms Planet', 'Accord Could Push A/C Out of Sweltering Indiaâs Reach ',....] 
</code></pre>

<p>Furthermore, you only need to strip spaces in each iteration. You don't need to do those replacements. So, you can do this with your <code>story_heading.text</code> instead:</p>

<pre><code>articles.append(story_heading.text.strip())
</code></pre>

<p>Which, can now give you a final solution looking like this: </p>

<pre><code>import requests
from bs4 import BeautifulSoup 

webpage = requests.get("http://www.nytimes.com/")
soup = BeautifulSoup(requests.get("http://www.nytimes.com/").text, "html.parser")
articles = [story_heading.text.strip() for story_heading in soup.find_all(class_="story-heading")]
print (articles)
</code></pre>

python string list split