【发布时间】:2017-12-12 05:55:27
【问题描述】:
我有一些 HTML 想用 Python 用 lxml 解析。页面上有许多元素,每个元素都代表一张海报。我想获取每个发布者的 ID,然后我可以从发布者的页面上刮下一条信息。目前海报的id存储在id属性中,所以我想用lxml来获取那个属性的值。
例如:
<div onclick="showDetail(9202)">
<div class="maincard narrower Poster" id="maincard_9202"> </div>
</div>
我想从 id 属性中获取“maincard_9202”,这样我就可以使用正则表达式来获取 9202。从那里,我可以使用这个值直接进入海报的页面,因为我知道 url重定向模式来自
https://nips.cc/Conferences/2017/Schedule?type=Poster(当前页面)到 https://nips.cc/Conferences/2017/Schedule?showEvent=9202(海报页)
我尝试使用以下代码:
from lxml import html
import requests
page = requests.get('https://nips.cc/Conferences/2017/Schedule?type=Poster')
tree = html.fromstring(page.content)
paper_numbers = tree.xpath('//div[@onclick]/id/')
但这会返回一个空列表。
这种情况下如何获取属性值?
【问题讨论】: