【问题标题】:Python Beautiful Soup Scraping Exact Content From ChartsPython Beautiful Soup 从图表中抓取准确的内容
【发布时间】:2015-07-19 14:51:03
【问题描述】:

在 python 中使用漂亮的汤,我希望能够从在线排序表中获取特定文本<a>/numbers<td>

http://www.nfl.com/stats/categorystats?archive=false&conference=null&role=OPP&offensiveStatisticCategory=null&defensiveStatisticCategory=INTERCEPTIONS&season=2014&seasonType=REG&tabSeq=2&qualified=false&Submit=Go

我已经尝试了大约一百万次,但无法弄清楚。

这是我能做的最好的:

from bs4 import BeautifulSoup
import urllib2
import requests
import pymongo
import re

soup = BeautifulSoup(urllib2.urlopen('http://www.nfl.com/stats/categorystats?archive=false&conference=null&role=OPP&offensiveStatisticCategory=null&defensiveStatisticCategory=INTERCEPTIONS&season=2014&seasonType=REG&tabSeq=2&qualified=false&Submit=Go').read())

find = soup('a', text="Miami Dolphins")

print find

我不知道如何在迈阿密海豚队之后找到/调用第 10 个(python 中的第 9 个)

标签。

表格代码如下所示:

<table id="result" style="width:100%" cellpadding="0" class"data-table1"
cellspacing="0">
   <caption class="thd1">...</caption>
   <tbody>...</tbody>
   <tbody>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">...</tr>
      <tr class="odd">...</tr>
      <tr class="even">
         <td>14</td>
         <td>
            <a href="/teams/miamidolphins/profile?team=MIA onclick=
            "s_objectID="http://www.nfl.com/teams.miamidolphins/profile?
            team=MIA_1";return this.s_oc?this.s_oc(e):true">Miami Dolphins</a>    *********I want to grab team name**********
         </td>
         <td>

         16

         </td>
         <td>24.2</td>
         <td>

         388

         </td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="sorted right">...</td>
           "


           14  ****I want to grab 10th number/<td> tag after team name****


                                        "
         </td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td>
         <td class="right">...</td
      </tr>

【问题讨论】:

    标签: python web-scraping beautifulsoup html-table


    【解决方案1】:

    试试这个

    import urllib2
    from lxml import etree
    
    url = 'http://www.nfl.com/stats/categorystats?archive=false&conference=null&role=OPP&offensiveStatisticCategory=null&defensiveStatisticCategory=INTERCEPTIONS&season=2014&seasonType=REG&tabSeq=2&qualified=false&Submit=Go'
    response = urllib2.urlopen(url)
    htmlparser = etree.HTMLParser()
    
    tree = etree.parse(response,htmlparser)
    
    text = tree.xpath('//a[contains(text(),"Miami Dolphins")]/parent::td/following-sibling::td[10]/text()')
    if text:
        print text[0].strip()
    

    【讨论】:

    • 哇。我希望我有你的技能!!你太棒了!!谢谢!!
    • 我不想成为一个痛苦...任何机会你可以解释每一行代码,所以我可以理解发生了什么?显然非常感谢您的帮助。
    • 如果我想使用 标签作为锚点而不是 标签怎么办?当我更改它时,它似乎不起作用。另外,如果我想使用来自不同 (在同一 URL 上)的标签作为锚点怎么办?一个很好的例子是一个包含多个表的玩家页面(传球表、冲球表和接收表...nfl.com/player/ryantannehill/2532956/careerstats
    • 您需要对提交的答案进行解释还是需要最后评论的解决方案
    • 主要是我上一条评论的解决方案。我的第一篇文章的解决方案非常完美。现在我试图在球员页面上做同样的事情,但遇到了麻烦......我尝试在球员职业统计中将 //a.."team name" 更改为 //td..."2014"(链接在我的最后评论),我无法让它工作。我也想跳到同一页面上的另一个表,但我不知道该怎么做(传球、冲刺、恢复)(在我的上一条评论中解释)。
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2022-09-30
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多