简体   繁体   English

python web抓漂亮的汤并添加到列表

[英]python web scraping beautiful soup and adding to a list

I am trying to learn to web scrape using Python and BeautifulSoup. 我正在尝试学习使用Python和BeautifulSoup进行网络抓取。 My problem is when attempting to add "scraped" items to a new list, only the final entry in the relevant tags is displaying when I print the lists. 我的问题是,当尝试将“已抓取的”项目添加到新列表时,当我打印列表时,仅显示相关标签中的最后一个条目。 How do I add each combination as a list item? 如何将每个组合添加为列表项?

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':'sortableRow oddRow'}) 奇数= soup.find_all('tr',attrs = {'class':'sortableRow奇数行'})

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

Assuming this isn't just a code formatting mistake, it's because your .append(...) calls aren't inside of your loops. 假设这不仅仅是代码格式化错误,那是因为您的.append(...)调用不在循环中。 Indent them to the same level as your variable settings (which aren't necessary if you just need those values during list creation) and you should get all relevant values. 将它们缩进到与变量设置相同的级别(如果在列表创建过程中只需要这些值,则不需要这样做),并且应该获得所有相关值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM