I am trying to learn python and have the below script that works, but I want to use the results to create a list that I can call outside the function. When I print list the right results are produced. The print x
does nothing.
import requests
from bs4 import BeautifulSoup
import urllib
import re
def hits_one_spider():
#page = 1
#while page <= max_pages:
url = "http://www.siriusxm.ca/hits-1-weekend-countdown/"
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.find('div', {'class': 'entry-content'}).findAll('li'):
#href = "http://www.siriusxm.ca/" + link.get('href')
title = link.string
#print(href)
#print(title)
return list
x = hits_one_spider()
print x
The problem is that you say return list
in the for loop. Thus it returns after the first iteration. Further you aren't actually returning it as a list, by doing it like that.
What you instead what is something like this:
lst = []
for link in soup.find('div', {'class': 'entry-content'}).findAll('li'):
lst.append(link.string)
return lst
Which results in returning (and printing) a list containing:
[
"Sam Smith – Stay With Me",
"Kongos – Come With Me Now",
"Iggy Azalea – Fancy feat. Charli XCX",
"OneRepublic – Love Runs Out",
"Magic! – Rude",
... and a lot more ...
"Oh Honey – Be Okay",
"Katy Perry – Birthday",
"Neon Trees – Sleeping With A Friend",
"Cher Lloyd – Sirens",
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.