我编写了一个脚本来解析网页中的一些特定内容。内容是静态的,使用我可以到达的请求模块。问题是,我想要获取的内容在一些html元素中,这些元素不是按常规格式格式化的。
我的脚本能够获取的是包含Mondays December 26th 2016 Horse Racing Tips等的标题。单词Mondays和年份2016总是出现在每个标题中。
现在,我想获取不同的种族技巧,如Sunshine Coast Race Tips的内容。在每个Mondays下面有多个竞赛提示。
一个这样的种族提示:
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())我怎样才能做到这一点?
发布于 2018-12-06 20:25:14
尝试下面的代码获取竞赛提示
for item in root.xpath('''(//div[b/font[.="Today's Race Tips:"]])[1]/following-sibling::div/b'''):
print(item.text_content())https://stackoverflow.com/questions/53658388
复制相似问题