【发布时间】:2018-02-11 20:50:16
【问题描述】:
我正在尝试解析页面中存在的所有查询字符串,以便使用该查询字符串我可以导航到特定页面。我尝试这样做的代码如下
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import datetime
import dateutil.parser
import time
import pytz
"""python espncricinfo library module https://github.com/dwillis/python-espncricinfo """
from espncricinfo.match import Match
from espncricinfo.exceptions import MatchNotFoundError, NoScorecardError
"""----time-zone-calculation----"""
time_zone = pytz.timezone("Asia/Kolkata")
datetime_today = datetime.datetime.now(time_zone)
datestring_today = datetime_today.strftime("%Y-%m-%d")
"""------URL of page to parse-------with a date of today-----"""
url = "http://www.espncricinfo.com/ci/engine/match/index.html?date=datestring_today"
"""eg. url = http://www.espncricinfo.com/ci/engine/match/index.html?date=2018-02-12"""
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
""""------parsing for matchno------"""
match_no = [x['href'].split('/',4)[4].split('.')[0] for x in
soup.findAll('a', href=True, text='Scorecard')]
for p in match_no:
""" where p is a match no, e.g p = '1122282'"""
m = Match(p)
m.latest_batting
print(m.latest_batting)
当我打印 match_no 时,我得到了输出:
['8890/scorecard/1118760/andhra-vs-tamil-nadu-group-c-vijay-hazare-trophy-2017-18/', '8890/scorecard/1118743/assam-vs-odisha-group-a-vijay-hazare-trophy-2017-18/', '8890/scorecard/1118745/bengal-vs-delhi-group-b-vijay-hazare-trophy-2017-18/', '8890/scorecard/1118763/chhattisgarh-vs-vidarbha-group-d-vijay-hazare-trophy-2017-18/']
此页面(http://www.espncricinfo.com/ci/engine/match/index.html?date=datestring_today") 包含当天发生的所有比赛的 match_no,我想修剪它以获得 7 位数字的 match_no [1118743,1118743.1118745....],我该怎么做? 所以使用 match_no 我可以将它传递给 Match() 以便我获得当天发生的特定比赛的详细信息。 PS 如果在新的一天没有比赛,那么 match_no 返回 none。
【问题讨论】:
-
我觉得你需要给我们一个例子列表。 soup.findAll 的原始回报是什么?
-
篇幅比较长,可以在这里下载模块github.com/dwillis/python-espncricinfo,还有什么需要了解的可以问我
-
我不需要。在此处向我们展示一个示例。为了您问题的完整性,我们至少需要一个 href 子集来了解您要解析的内容。
-
嘿,查看此链接shrib.com/#VvY5F2Y1QiQpFzT8Ly-Y,我发布了今天的空匹配数据,对不起。
标签: python python-3.x parsing web-scraping beautifulsoup