【问题标题】:python web scraping beautiful soup and adding to a listpython web抓取漂亮的汤并添加到列表中
【发布时间】:2019-02-23 01:25:04
【问题描述】:

我正在尝试学习使用 Python 和 BeautifulSoup 进行网络抓取。我的问题是在尝试将“已抓取”项目添加到新列表时,当我打印列表时,仅显示相关标签中的最终条目。如何将每个组合添加为列表项?

import requests

    standings = requests.get('http://games.espn.com/ffl/tools/finalstandings?leagueId=379978&seasonId=2012')


from bs4 import BeautifulSoup

soup = BeautifulSoup(standings.text, 'html.parser')

## Ask BeautifulSoup to find all of the records

pat = soup.find_all('tr', attrs={'class':'sortableRow evenRow'})
teams = []
for x in pat:
        name1 = x.find('a').text
        record1 = x.find('td', {'class':'sortableREC'}).text
        pf1 = x.find('td', {'class':'sortablePF'}).text
        pa1 = x.find('td', {'class':'sortablePA'}).text
        pfg1 = x.find('td', {'class':'sortablePFG'}).text
        pag1 = x.find('td', {'class':'sortablePAG'}).text
        diff1 = x.find('td', {'class':'sortableDIFF'}).text


 teams.append((name1, record1, pf1, pa1, pfg1, pag1, diff1))

odd =soup.find_all('tr', attrs={'class':'sortableRowoddRow'})

teams2 = []
for team in odd:
        name2 = team.find('a').text
        record2 = team.find('td', {'class':'sortableREC'}).text
        pf2 = team.find('td', {'class':'sortablePF'}).text
        pa2 = team.find('td', {'class':'sortablePA'}).text
        pfg2 = team.find('td', {'class':'sortablePFG'}).text
        pag2 = team.find('td', {'class':'sortablePAG'}).text
        diff2 = team.find('td', {'class':'sortableDIFF'}).text
teams2.append((name2, record2, pf2, pa2, pfg2, pag2, diff2))

【问题讨论】:

    标签: python html web beautifulsoup screen-scraping


    【解决方案1】:

    假设这不仅仅是代码格式错误,这是因为您的 .append(...) 调用不在循环内。将它们缩进到与您的变量设置相同的级别(如果您在创建列表期间只需要这些值,则没有必要),您应该获得所有相关值。

    【讨论】:

    • 感谢您的帮助!有一种感觉是格式中的东西。在循环中包含附加是要走的路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2019-05-23
    • 2022-01-08
    • 2013-11-25
    • 2020-01-20
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多