Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to give variables to a list?
#1


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.
Reply


Messages In This Thread
How to give variables to a list? - by HiImNew - Nov-17-2017, 05:31 AM
RE: How to give variables to a list? - by heiner55 - Nov-17-2017, 05:58 AM
RE: How to give variables to a list? - by HiImNew - Nov-17-2017, 07:03 AM
RE: How to give variables to a list? - by heiner55 - Nov-17-2017, 07:32 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020