无法使用python和beautifulsoup在网页中抓取某些href

时间:2016-08-22 11:03:15

标签: python html beautifulsoup tabpage

我目前正在使用Python 3.4和bs4抓取网页,以便收集塞尔维亚在Rio2016中所做的匹配结果。因此,网址here包含指向她所播放的所有匹配结果的链接,例如this

然后我发现该链接位于html源代码中,如下所示:

<a href="/en/volleyball/women/7168-serbia-italy/post" ng-href="/en/volleyball/women/7168-serbia-italy/post">
    <span class="score ng-binding">3 - 0</span>
</a>

但经过多次试验后,href="/en/volleyball/women/7168-serbia-italy/post"从未显现过。然后我尝试运行以下代码从url获取所有href:

from bs4 import BeautifulSoup
import requests

Countryr = requests.get('http://rio2016.fivb.com/en/volleyball/women/teams/srb-serbia#wcbody_0_wcgridpadgridpad1_1_wcmenucontent_3_Schedule')
countrySoup = BeautifulSoup(Countryr.text)

for link in countrySoup.find_all('a'):
    print(link.get('href'))

然后发生了一件奇怪的事情。 href="/en/volleyball/women/7168-serbia-italy/post"根本不包含在输出中。

我发现此href位于此网址旁边的一个标签页href="#scheduldedOver"中,并且由以下HTML代码控制:

<nav class="tabnav">
    <a href="#schedulded" ng-class="{selected: chosenStatus == 'Pre' }" ng-click="setStatus('Pre')" ng-href="#schedulded">Scheduled</a>
    <a href="#scheduldedLive" ng-class="{selected: chosenStatus == 'Live' }" ng-click="setStatus('Live')" ng-href="#scheduldedLive">Live</a>
    <a href="#scheduldedOver" class="selected" ng-class="{selected: chosenStatus == 'Over' }" ng-click="setStatus('Over')" ng-href="#scheduldedOver">Complete</a>
</nav>

那么如何在标签页中使用BeautifulSoup获取href?

2 个答案:

答案 0 :(得分:1)

数据是动态创建的,如果您查看实际来源,可以看到How to warm up an ASP.NET MVC application on IIS 7.5?模板。

你仍然可以通过模仿ajax调用获得json格式的所有信息,在源代码中yuuuuou也可以看到如下的div:

<div id="AngularPanel" class="main-wrapper" ng-app="fivb"
data-servicematchcenterbar="/en/api/volley/matches/341/en/user/lives"
data-serviceteammatches="/en/api/volley/matches/WOG2016/en/user/team/3017"
data-servicelabels="/en/api/labels/Volley/en" 
data-servicelive="/en/api/volley/matches/WOG2016/en/user/live/">

使用data-servicematchcenterbar href将为您提供所有信息:

from bs4 import BeautifulSoup
import requests
from urlparse import urljoin

r = requests.get('http://rio2016.fivb.com/en/volleyball/women/teams/srb-serbia#wcbody_0_wcgridpadgridpad1_1_wcmenucontent_3_Schedule')
soup = BeautifulSoup(r.content)

base = "http://rio2016.fivb.com/"

json = requests.get(urljoin(base, soup.select_one("#AngularPanel")["data-serviceteammatches"])).json()

在json中,您将看到如下输出:

{"Id": 7168, "MatchNumber": "006", "TournamentCode": "WOG2016", "TournamentName": "Women's Olympic Games 2016",
        "TournamentGroupName": "", "Gender": "", "LocalDateTime": "2016-08-06T22:35:00",
        "UtcDateTime": "2016-08-07T01:35:00+00:00", "CalculatedMatchDate": "2016-08-07T03:35:00+02:00",
        "CalculatedMatchDateType": "user", "LocalDateTimeText": "August 06 2016",
        "Pool": {"Code": "B", "Name": "Pool B", "Url": "/en/volleyball/women/results and ranking/round1#anchorB"},
        "Round": 68,
        "Location": {"Arena": "Maracanãzinho", "City": "Maracanãzinho", "CityUrl": "", "Country": "Brazil"},
        "TeamA": {"Code": "SRB", "Name": "Serbia", "Url": "/en/volleyball/women/teams/srb-serbia",
                  "FlagUrl": "/~/media/flags/flag_SRB.png?h=60&w=60"},
        "TeamB": {"Code": "ITA", "Name": "Italy", "Url": "/en/volleyball/women/teams/ita-italy",
                  "FlagUrl": "/~/media/flags/flag_ITA.png?h=60&w=60"},
        "Url": "/en/volleyball/women/7168-serbia-italy/post", "TicketUrl": "", "Status": "Over", "MatchPointsA": 3,
        "MatchPointsB": 0, "Sets": [{"Number": 1, "PointsA": 27, "PointsB": 25, "Hours": 0, "Minutes": "28"},
                                    {"Number": 2, "PointsA": 25, "PointsB": 20, "Hours": 0, "Minutes": "25"},
                                    {"Number": 3, "PointsA": 25, "PointsB": 23, "Hours": 0, "Minutes": "27"}],
        "PoolRoundName": "Preliminary Round", "DayInfo": "Weekend Day",
        "WeekInfo": {"Number": 31, "Start": 7, "End": 13}, "LiveStreamUri": ""},

你可以解析那些你需要的东西。

答案 1 :(得分:1)

感谢您的帮助,我现在可以获得正确的网址。对我来说这是一次很好的学习经历。非常感谢:)

from bs4 import BeautifulSoup
import requests

Countryr = requests.get('http://rio2016.fivb.com/en/volleyball/women/teams/srb-serbia#wcbody_0_wcgridpadgridpad1_1_wcmenucontent_3_Schedule')
countrySoup = BeautifulSoup(Countryr.text)

for link in countrySoup.find_all('div', {'id': 'AngularPanel'}):
    linkUrl = link.get('data-serviceteammatches')

json = requests.get('http://rio2016.fivb.com' + linkUrl).json()

for item in json:
    print(item.get('Url'))

输出:

/en/volleyball/women/7168-serbia-italy/post
/en/volleyball/women/7172-serbia-puerto rico/post
/en/volleyball/women/7177-usa-serbia/post
/en/volleyball/women/7181-china-serbia/post
/en/volleyball/women/7187-serbia-netherlands/post
/en/volleyball/women/7195-russia-serbia/post
/en/volleyball/women/7198-serbia-usa/post
/en/volleyball/women/7200-china-serbia/post