【发布时间】:2017-12-22 23:54:48
【问题描述】:
以下代码使用的是scrapy + scrapy-splash + Python。 我正在尝试从这个网站提取即将到来的比赛(包括:球队名称、锦标赛名称、开始时间):https://www.hltv.org/matches
我在回调“解析”函数中的代码是:
match_days = response.xpath("//div[@class = 'upcoming-matches']//div[@class = 'match-day']")
for match in match_days.xpath("./a"):
print(match.extract())
# tournament_name = match.xpath(".//td[@class='event']//span[@class='event-name']/text()").extract_first()
# team1_name = match.xpath(".//td[@class='team-cell'][1]//div[@class='team']/text()").extract_first()
它应该让我得到每个“”元素的内容(即应该看起来像这样,例如:
<a href="/matches/2318355/dkiss-vs-psychoactive-prowince-winner-winner-of-the-future-2017" class="a-reset block upcoming-match standard-box" data-zonedgrouping-entry-unix="1514028600000">
<table class="table">
<tbody>
<tr>
<td class="time">
<div class="time" data-time-format="HH:mm" data-unix="1514028600000">12:30</div>
</td>
<td class="team-cell">
<div class="line-align">
<img alt="DKISS" src="https://static.hltv.org/images/team/logo/8657" class="logo" title="DKISS">
<div class="team">DKISS</div>
</div>
</td>
<td class="vs">vs</td>
<td class="team-cell">
<div class="team">PSYCHOACTIVE/proWince winner</div>
</td>
<td class="event"><img alt="Winner of the Future 2017" src="https://static.hltv.org/images/eventLogos/3464.png" class="event-logo" title="Winner of the Future 2017"><span class="event-name">Winner of the Future 2017</span></td>
<td class="star-cell">
<div class="map-text">bo3</div>
</td>
</tr>
</tbody>
</table>
</a>
但我只为每个“”得到这个:
<a href="/matches/2318355/dkiss-vs-psychoactive-prowince-winner-winner-of-the-future-2017" class="a-reset block upcoming-match standard-box" data-zonedgrouping-entry-unix="1514028600000">
</a>
我在scrapy shell中试过这个,结果一样。
我尝试了 Chrome 开发者工具,我可以看到 innerHTML 属性中每个“”的所有内容。
我不认为问题出在“”,因为我已经了解到它在某些情况下被省略并由 Web 浏览器添加,因为当我从"response" "" 就在那里(顺便说一句,我使用 lua 脚本通过 scrapy-splash 向 url 发出 POST 请求并获取 html 页面)
有人知道为什么会这样吗?在过去的几天里,我一直没有回答,我也不知道还有什么要测试的东西来弄清楚为什么会发生这种情况而不应该发生。
谢谢。
【问题讨论】:
标签: python xpath scrapy scrapy-spider scrapy-splash