【问题标题】:Splitting and slicing scraped python lists拆分和切片抓取的 python 列表
【发布时间】:2021-11-18 16:36:00
【问题描述】:

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

我想在数据框中执行此操作。我已经刮掉了他们的胜算。

我想将球队和赔率放在不同的列中。

我想要单独的游戏在不同的行上。

我已设法以以下形式返回列表:

['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)

'''

【问题讨论】:

    标签: python web-scraping split slice


    【解决方案1】:

    嗯,你可以使用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
    

    【讨论】:

      猜你喜欢
      • 2014-11-02
      • 2010-12-14
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 2017-06-24
      • 2015-02-19
      相关资源
      最近更新 更多