从站点获取 table

Get table from site

我想在这个网站上得到 table "Partite":
http://it.soccerway.com/national/italy/serie-a/20142015/regular-season/r27139/

所以我创建了这个代码:

Dim HTML As String = New WebClient().DownloadString(URLs(MetroComboBox2.SelectedIndex))
    Dim URL_Params As String = "&callback_params=" & Regex.Match(HTML, "'block_competition_matches_summary', ({[\w\s"",:]+})").Groups(1).ToString
    Dim Base_URL As String = "http://it.soccerway.com/a/block_competition_matches_summary?block_id=page_competition_1_block_competition_matches_summary_6"
    Dim Giornata_URL As String = Base_URL & URL_Params & "&action=changeView&params={""view""%3A1}"

with Html 变量我得到我之前发布的 link,在 URL_Params 我试图匹配 div class "block_competition_matches_summary".

但显然正则表达式没有捕捉到元素。 所以我在 Giornata_Url 中组装所有变量。我错在哪里?

我猜你是想匹配网页的这一部分?

'block_competition_matches_summary', {"page":0,"bookmaker_urls":{"13":[{"link":"http:\/\/www.bet365.com\/home\/?affiliate=365_308136","name":"Bet 365"}]},"block_service_id":"competition_summary_block_competitionmatchessummary","round_id":27139,"outgroup":false,"view":2}

永远不会被这个正则表达式匹配:

'block_competition_matches_summary', ({[\w\s",:]+})

数据结构包含嵌套大括号;不符合字符 class [\w\s",:].

使用正则表达式匹配嵌套大括号并不容易。哪个右括号应该结束比赛?

一个简单的替代方法是将匹配项的结尾锚定到行尾。这个正则表达式工作正常:

'block_competition_matches_summary', (\{.*?\})\);\n

解释:

  • ( - 捕获子模式的开始
  • \{ - 请转义大括号,因为它们在正则表达式语法中具有特殊含义
  • .*? - 任意数量的字符,非贪婪(这在这里很重要)
  • \} - 再次转义大括号
  • ) - 捕获子模式结束
  • \) - 文字字符:右括号
  • ; - 文字字符:分号
  • \n - 换行符

我建议您将它与 RegexOptions.Singleline 结合使用,以防万一您要匹配的表达式中出现换行符。

最后评论:在将结果字符串添加到 URL_Params 之前,请对其进行 URL 编码。这构成了完整的陈述:

Dim URL_Params As String = "&callback_params=" & WebUtility.UrlEncode(Regex.Match(HTML, "'block_competition_matches_summary', (\{.*?\})\);\n", RegexOptions.Singleline).Groups(1).Value)