【发布时间】:2020-05-30 13:34:24
【问题描述】:
我正在尝试设置一个 Scrapy 选择器以从 Trezor 支持的硬币页面 (https://trezor.io/coins/) 获取表格上的一些数据:
In [1]: import requests
...: from scrapy.selector import Selector
...: req = requests.get('https://trezor.io/coins/').content
...: xs = '//*[@id="content"]/tr'
...: sel = Selector(text=req).xpath(xs)
In [2]: sel.extract_first()
Out[2]: '<tr class="coin " data-href="./#BTC" id="BTC"></tr>'
选择器不应该带来tr 元素及其内部的所有内容(在这种情况下,六个td 元素具有更多内部元素?当我尝试手动访问td 元素时(使用@ 987654328@ 或xs = '//*[@id="content"]/tr[1]/td[1]'),我得到的只是一个空列表。我也尝试获取child nodes,但无济于事。
Cf. 在Wikipedia's 主页上提取,您可以在其中获取指定容器内的所有内容:
In [3]: req2 = requests.get('https://en.wikipedia.org/wiki/Main_Page').content
...: xd = '//*[@id="mp-welcomecount"]'
...: sel2 = Selector(text=req2).xpath(xd)
In [4]: sel2.extract_first()
Out[4]: '<div id="mp-welcomecount">\n<div id="mp-welcome">Welcome to <a href="/wiki/Wikipedia" title="Wikipedia">Wikipedia</a>,</div>\n<div id="mp-free">the <a href="/wiki/Free_content" title="Free content">free</a> <a href="/wiki/Encyclopedia" title="Encyclopedia">encyclopedia</a> that <a href="/wiki/Help:Introduction" title="Help:Introduction">anyone can edit</a>.</div>\n<div id="articlecount"><a href="/wiki/Special:Statistics" title="Special:Statistics">6,088,421</a> articles in <a href="/wiki/English_language" title="English language">English</a></div>\n</div>'
为什么在 Trezor 的情况下我只得到 tr 元素,我该如何更正我的代码以将其中包含的所有内容都包含在内?
【问题讨论】:
标签: python html xpath web-scraping scrapy