【问题标题】:How can I parse querystring from webpage?如何解析网页中的查询字符串?
【发布时间】: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


【解决方案1】:

首先,您的代码很难阅读。你需要让你的代码呼吸,让它吸引其他人阅读。

其次,引起问题的可能是这一行:

match_no = [x['href'].split('/',4)[4].split('.')[0] for x in soup.findAll('a', href=True, text='Scorecard')]

它也很难阅读。从 URL 中解析匹配 id 的方式要好得多,也更易读。

这是应该工作的示例。我确实为比赛选择了临时日期:

import re

import pytz
import requests
import datetime
from bs4 import BeautifulSoup
from espncricinfo.exceptions import MatchNotFoundError, NoScorecardError
from espncricinfo.match import Match

"""python espncricinfo library module https://github.com/dwillis/python-espncricinfo """
# from espncricinfo.match import Match


def get_match_id(link):
    match_id = re.search(r'([0-9]{7})', link)
    if match_id is None:
        return None
    return match_id.group()

# ----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"

r = requests.get(url)

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

spans = soup.findAll('span', {"class": "match-no"})

matches_ids = []

for s in spans:
    for a in s.findAll('a', href=lambda href: 'scorecard' in href):
        match_id = get_match_id(a['href'])
        if match_id is None:
            continue
        matches_ids.append(match_id)


# ------parsing for matchno------
for p in matches_ids:
    # where p is a match no, e.g p = '1122282'
    m = Match(p)
    m.latest_batting
    print(m.latest_batting)

现在,我没有你在这里使用的所有库,但这应该让你知道如何去做。

再一次,我的建议是空行是你的朋友。他们肯定是读者的朋友。让你的代码“呼吸”。

【讨论】:

  • 对不起@ikac 我的错误代码,我再也不会重复这个错误了。谢谢你的回答,我得到了我想要的输出,喜欢你简单的方法,学到了很多新东西,再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 2019-05-03
  • 2011-02-27
相关资源
最近更新 更多