拆分和切片抓取的python列表



我正在尝试创建一个球队及其赔率的显示。

我想在数据框中做。我把刮掉了他们的几率。

我想把球队和赔率分开列。

我想要单独的游戏在单独的行。

我已经按照下面的格式返回了列表:

['Buffalo Bills 1.30 Washington Football Team 3.50',
'Kansas City Chiefs 1.35 Los Angeles Chargers 3.25',]

我需要返回列表或切片,使其显示如下:

[['San Francisco 49ers, 1.62, Green Bay Packers, 2.30'],
['Dallas Cowboys, 1.57, Philadelphia Eagles, 2.40],]

迄今为止使用的代码如下:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',options=chrome_options)
url = str("https://www.ladbrokes.com.au/sports/american-football/nfl")
wd.get(url)
time.sleep(5)
html = wd.page_source
html 
SCROLL_PAUSE_TIME = 1
# Get scroll height
last_height = wd.execute_script("return document.body.scrollHeight")
while True:
#Scroll down to bottom
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
games = wd.find_elements_by_class_name("sports-market-primary__prices-inner")
# Calculate new scroll height and compare with last scroll height
new_height = wd.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
result=[]
for  i in games:
var = i.text
var = str(var).replace('n',' ')#.split()
result.append(var)
'''

那么,您可以使用re模块。下面是示例代码:

import re

result = [
"Buffalo Bills 1.30 Washington Football Team 3.50",
"Kansas City Chiefs 1.35 Los Angeles Chargers 3.25",
]
pattern = "([A-Za-z ]+) (d+.?d*) ([A-Za-z ]+) (d+.?d*)"
pre_compiled = re.compile(pattern)
for i in result:
l = list(*re.findall(pattern, i))
ans = ", ".join(l)
print(l)
print(ans)
print()

输出:

['Buffalo Bills', '1.30', 'Washington Football Team', '3.50']
Buffalo Bills, 1.30, Washington Football Team, 3.50
['Kansas City Chiefs', '1.35', 'Los Angeles Chargers', '3.25']
Kansas City Chiefs, 1.35, Los Angeles Chargers, 3.25

最新更新