Python和lxml.html获取“元素”输出问题

2024-10-02 22:29:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从html文件中获取数据。我使用的代码似乎可以正常工作,但并不像我预期的那样。我可以得到一些项目,但不是全部,我想知道这是否与我试图读取的文件的大小有关。在

我正在尝试解析this webpage的源代码。在

这页有4500行长,所以它是一个相当好的大小。我一直在使用这个页面,因为我想确保代码在大文件上工作。在

我使用的代码是:

import lxml.html
import lxml
import urllib2

webHTML = urllib2.urlopen('http://hobbyking.com/hobbyking/store/__39036__Turnigy_Multistar_2213_980Kv_14Pole_Multi_Rotor_Outrunner.html').read()
webHTML = lxml.html.fromstring(webHTML)
productDetails = webHTML.get_element_by_id('productDetails')
for element in productDetails:
    print element.text_content()

当我使用元素_id为'mm3'或接近顶部的元素时,这将给出预期的输出,但是如果我使用元素_id'productDetails',则没有输出。至少我现在的设置是这样的。在


Tags: 文件项目代码importid元素htmlelement
1条回答
网友
1楼 · 发布于 2024-10-02 22:29:39

恐怕lxml.html无法解析这个特定的HTML源代码。它将带有id="productDetails"h3标记解析为空元素(这在default "recover" mode中):

<h3 class="productDescription2" id="productDetails" itemprop="description"></h3>

^{} parser切换到^{}(这是一个非常宽大的):

^{pr2}$

印刷品:

Looking for the ultimate power system for your next Multi-rotor project? Look no further!The Turnigy Multistar outrunners are designed with one thing in mind - maximising Multi-rotor performance! They feature high-end magnets, high quality bearings and all are precision balanced for smooth running, these motors are engineered specifically for multi-rotor use.These include a prop adapter and have a built in aluminium mount for quick and easy installation on your multi-rotor frame.

outrunner

...

相关问题 更多 >