Python Forum

Full Version: How to give variables to a list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


Code Input:
>>> import bs4 as bs
>>> import urllib.request
>>> sauce = urllib.request.urlopen('https://globenewswire.com/Search/NewsSearch?lang=en&exchange=Nasdaq').read()
>>> soup = bs.BeautifulSoup(sauce,'lxml')
>>> for div in soup.find_all('div', class_='results-link'):
str = ('https://globenewswire.com' + div.h1.a['href'])
print(str.splitlines())

Code Output:
['https://globenewswire.com/news-release/2017/11/17/1194477/0/en/Latam-Resources-Pty-Limited-Announces-Ownership-Interest-in-NRG-Metals-Inc.html']
['https://globenewswire.com/news-release/2017/11/17/1194476/0/en/Montero-Channel-Sampling-Returns-14-Metres-of-1-93-Lithium-Oxide-at-the-Soris-Lithium-Project-in-Namibia.html']
['https://globenewswire.com/news-release/2017/11/17/1194475/0/en/Two-law-firms-unite-to-launch-African-Connection-Protection-to-rescue-investors-from-419-Scams.html']
['https://globenewswire.com/news-release/2017/11/17/1194474/0/en/Genmab-Achieves-USD-50-Million-Sales-Milestone-in-DARZALEX-daratumumab-Collaboration-with-Janssen.html']
['https://globenewswire.com/news-release/2017/11/17/1194473/0/en/Stitch-Fix-Announces-Pricing-of-Initial-Public-Offering.html']
['https://globenewswire.com/news-release/2017/11/17/1194472/0/en/scPharmaceuticals-Inc-Announces-Pricing-of-Initial-Public-Offering.html']
['https://globenewswire.com/news-release/2017/11/17/1194471/0/en/Devonian-Announces-the-Filing-of-New-Patent-Application-for-Thykamine.html']
['https://globenewswire.com/news-release/2017/11/17/1194470/0/en/Doubletree-By-Hilton-Carson-Offers-Active-Duty-Military-And-Veterans-An-Extra-20-Percent-Off-Los-Angeles-Chargers-Room-And-Ride-Package-Supports-Chargers-Salute-To-Service-On-Novem.html']
['https://globenewswire.com/news-release/2017/11/17/1194468/0/en/Global-100-Australian-Law-Firm-Allens-Selects-iManage-Work-Product-Management.html']
['https://globenewswire.com/news-release/2017/11/17/1194469/0/en/JCPenney-Releases-2017-Corporate-Social-Responsibility-Report.html']

Youtube shows how to split up the urls Beautiful Soup 4 is giving me into 10 different lines encased in brackets and single quotes. I've gotten to that point so far. In what way do I assign each of these urls its own variable.

I've tried this with no indentation after specifying the string:
print(str.splitlines(3))
print(str.splitlines(5))
print(str.splitlines(7))

with the numbers 3 5 and 7 representing the 3rd 5th and 7th line, but when I do this every time only the first url is printed. Any idea what I'm doing wrong with the code or a method for assigning variables to the urls would be much appreciated. Thank you! Tongue

P.S. if this code looks familiar its because I lost my account password and for some reason my email was not valid in requesting a reset.
str = "000\n111\n222\n333\n444\n555\n"
lis = str.splitlines()


print(lis[3])
print(lis[5])
Output:
333 555
(Nov-17-2017, 05:58 AM)heiner55 Wrote: [ -> ]
str = "000\n111\n222\n333\n444\n555\n"
lis = str.splitlines()


print(lis[3])
print(lis[5])
Output:
333 555

>>> print(lis[3])
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print(lis[3])
IndexError: list index out of range

>>> for div in soup.find_all('div', class_='results-link'):
str = ('https://globenewswire.com' + div.h1.a['href'])
lis = str.splitlines()
print(lis[7])


Traceback (most recent call last):
File "<pyshell#16>", line 4, in <module>
print(lis[7])
IndexError: list index out of range


tried that and got error both times. That is the error it sends back to me. I tried two different list values. Any idea why it may not be working? I'm on Python 3.6.3 if that makes any difference.
Sorry.
If you got an error with my tiny sample,
I do not know what happens on your pc.

Python 3.6.x is fine.