次のコードでは、scrapy + scrapy-splash + Pythonを使用しています。 https://www.hltv.org/matchesXpathが<table>の内容を<a>に返さない(<tbody>は問題ありません)
コールバック「パース」関数での私のコードは次のとおりです:
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シェルと同じ結果でこれを試してみました。
私はChromeデベロッパーツールを試しましたが、innerHTMLプロパティの各 "<>"のすべてのコンテンツを見ることができます。
「< tbody>」の問題はないと私は理解しています。一部のケースでは省略され、Webブラウザによって追加されたことがわかりました。なぜなら、返されたページのHTMLコンテンツを"< tbody>"があります(ちなみに、私はscrapy-splashでURLをPOSTリクエストしてhtmlページを取得するためにluaスクリプトを使用します)
これはなぜ起こっているのですか? ?私は過去2〜3日を無回答で過ごしました。また、どうしてこのようなことが起こってはいけないのかを理解するために、何を試すべきかについて私は考えていません。
ありがとうございます。
。はい、これは完全に動作します。私はCSSの代わりにXpathで書き直しました。しかし、私はまだあなたが 'テーブル'要素を反復する必要がある理由を理解しておらず、 'a'要素(すなわち "tables = day.css( 'table')"を "tables = day.css( 'a')は結果に出力するすべての変数に "None"を返します。 – Vasko
ブラウザでソースコードをチェックすると、 'a'の中に' table'がありますので、しかし、 'print(day.extract())'をチェックすると、 'a'は空で' table'は 'a'の中ではなく' a'の後にはありません。いくつかのオプション(ヘッダ、クッキー)が不完全な場合や、HTMLが正しく構築されておらず、ブラウザがそれをスクラピーとは異なる方法で解釈した場合、ページは異なるHTMLを送信するかもしれません。 – furas