代码之家  ›  专栏  ›  技术社区  ›  SIM

无法从某些复杂的html元素中提取特定内容

  •  1
  • SIM  · 技术社区  · 7 年前

    我的脚本可以抓取的是包含 Mondays December 26th 2016 Horse Racing Tips Mondays 2016 总是出现在每个标题中。

    现在,我想抓取不同目录下的内容 喜欢 Sunshine Coast Race Tips . 每个选项下都有多个比赛提示 星期一 .

    其中一个比赛提示:

    Sunshine Coast Race Tips:
    
    Race 1: 7, 5, 4, 3 - Winner (1) $1.30 Exacta $1.90 Trifecta $4.10
    Race 2: 2, 4, 3, 8 - Winner (1) $3.40 Exacta $62.70 Trifecta $116.10 First 4 $158.80
    Race 3: 4, 10, 5, 13 - 2nd and 4th - Loss
    

    这是我迄今为止的尝试:

    import requests
    from lxml.html import fromstring
    
    url = "https://www.freehorseracingtipsaustralia.com/mondays-horse-racing-results-2016"
    
    res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
    root = fromstring(res.text)
    for item in root.xpath("//b[starts-with(.,'Mondays')]"):
        print(item.text_content())
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Andersson    7 年前

    尝试下面的代码获得比赛技巧

    for item in root.xpath('''(//div[b/font[.="Today's Race Tips:"]])[1]/following-sibling::div/b'''):
        print(item.text_content())
    
    推荐文章