Python Forum
How to append multiple <class 'str'> into a single List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append multiple <class 'str'> into a single List
#1
I have a variable called "link_list" that prints out multiple game-id URLs which I, using pythonic means, procured from a sports website. The terminal output looks something like this:

print(link_list)


https://www.pcb.com.pk/match_detail.php?match_id=21782
https://www.pcb.com.pk/match_detail.php?match_id=21790
https://www.pcb.com.pk/match_detail.php?match_id=21798
https://www.pcb.com.pk/match_detail.php?match_id=21812
https://www.pcb.com.pk/match_detail.php?match_id=21822
If I print using print(type(variable)) it shows the following <class 'str'> fields for each line

print(type(link_list))

<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
Now what I want is, to merge these results (URLs) in the form of a list so I can then run a selenium code snippet later on to get relevant data from these. How do I go about doing this? Any assistance on the matter would be appreciated.


Also here's my code for how I am procuring these values [THIS IS JUST FOR REFERENCE ONLY]:

#Importing all the necessary Libraries
import PySimpleGUI as sg
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import urllib.request
from selenium.webdriver.support.ui import Select
import pandas as pd
import re
import itertools


# Developing Simple Front End
label = [sg.Text("Please Enter Link")]
link = [sg.Input(enable_events=True, key='link')]
scrape_btn = [sg.Button("Scrape")]
exit_btn = [sg.Button("Exit")]
select_discipline = ['batting', 'bowling']
dropdown_discipline = [sg.Combo(select_discipline, enable_events=True, key='selection')]

#Developing Layout and Window Pane
layout = [label, link, dropdown_discipline, scrape_btn, exit_btn]
window = sg.Window("PCB Domestic Numbers", layout)


while True:
	event, values = window.read()
	
	if event == "Scrape":
		combo = values['selection']
		link = values['link']
		if combo == 'batting':
			driver = webdriver.Chrome()
			driver.get(link)
			select = Select(driver.find_element_by_name('new_page_limit'))
			select.select_by_value('all')
			html = driver.page_source
			soup = BeautifulSoup(html, "html.parser")
			target_name = html[html.find("<h1 class="):html.find("</h1>")]
			name = target_name.split('by ')[1]
			link_list = []
			for link in soup.findAll('a'):
				if "match_id" in link.get('href'):
					link_list = link
					link_list = str(link_list)
					start = '="'
					end = '">'
					link_list = link_list.split(start)[1].split(end)[0]
					print(type(link_list))
			driver.close()
			window.close()

		elif combo == 'bowling':
			print(combo)
		

	if event == "Exit" or event == sg.WIN_CLOSED:
		break

window.close()
Reply
#2
In your loop where you're setting link_list, append it to a list that it outside the loop.

You've created such a list on line 41. But line 41 is useless because the variable is overwritten later by line 44.

Perhaps a bit more like...
            start = '="'
            end = '">'
            link_list = []
            for link in soup.findAll('a'):
                if "match_id" in link.get('href'):
                    link_list.append(str(link).split(start)[1].split(end)[0])
ahmedwaqas92 likes this post
Reply
#3
(Jan-07-2021, 07:02 AM)bowlofred Wrote: In your loop where you're setting link_list, append it to a list that it outside the loop.

You've created such a list on line 41. But line 41 is useless because the variable is overwritten later by line 44.

Perhaps a bit more like...
            start = '="'
            end = '">'
            link_list = []
            for link in soup.findAll('a'):
                if "match_id" in link.get('href'):
                    link_list.append(str(link).split(start)[1].split(end)[0])

Thank you so much, it solved the problem as intended. Much appreciated :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ploting single column with multiple category drunkenneo 1 328 May-26-2021, 04:51 PM
Last Post: jefsummers
  Better way to append frames from a video to multiple lists? Balaganesh 0 212 May-13-2021, 07:37 AM
Last Post: Balaganesh
  Parse String between 2 Delimiters and add as single list items lastyle 5 391 Apr-11-2021, 11:03 PM
Last Post: lastyle
  apendng to a list within a class gr3yali3n 4 360 Feb-16-2021, 06:30 AM
Last Post: buran
  convert List with dictionaries to a single dictionary iamaghost 3 497 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Inserting multiple rows in a single request. swaroop 2 455 Jan-11-2021, 01:34 PM
Last Post: swaroop
  Fetching data from multiple tables in a single request. swaroop 0 301 Jan-09-2021, 04:23 PM
Last Post: swaroop
  Undo interation to make a single list? DustinKlent 2 418 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  How to append to list a function output? rama27 5 817 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Append list into list within a for loop rama27 2 699 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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