English 中文(简体)
如何以给给定 href 更快的方式获得第一个元素
原标题:How to get first element with the given href faster

我有工作样本, 以给定的 href 生成 html 元素列表。 但我只需要第一个, 速度很重要, 如何优化这个代码?

import lxml.html
input = """<div class="post" style="height: 36px; ">
some div text 
<a href="http://site.com/" target="_blank">Look here</a>, 
some div text also </div>
"""
root = lxml.html.fromstring(input)
el = root.xpath("//*[@href= http://site.com/ ]")[0]
el.text
问题回答

您可以在您的 xpath 表达式中使用索引 : (//* [@href= http://site.com/]) [1]





相关问题
Curve fitting with lmfit for Mossbauer spectroscopy

Im pretty new to lmfit and I keep running into this error. My dataset is pretty large ~27000 points of data from I gather: my error msg import numpy as np import matplotlib.pyplot as plt from lmfit ...

Python SocketServer: sending to multiple clients?

Well, I m trying to build a small python prgram with a SocketServer that is supposed to send messages it receives to all connected clients. I m stuck, I don t know how to store clients on the ...

Is there no mysql connector for python 2.7 on windows

While I see a bunch of links/binaries for mysql connector for python 2.6, I don t see one for 2.7 To use django, should I just revert to 2.6 or is there a way out ? I m using windows 7 64bit django ...

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I m not sure how to do it, except for replacing the widget itself. Currently, I can display an image like so: import Tkinter as ...

PyScripter for Python 2.7

How to run PyScripter if you have Python 2.7 installed? There is a command line parameter for Pyscipter to tell it which pythonXX.dll to use, but I can t get this working.

How to set time limit on raw_input

in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped?

热门标签