擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/2/library/functions.html#filter" rel="nofollow">^{<cd1>}</a>和<a href="http://docs.python.org/2/library/stdtypes#str.isdigit" rel="nofollow">^{<cd2>}</a>,下面的代码从每一行提取数字。你知道吗</p>
<pre><code>>>> lines = '''<tr id="product_34980" class="even">
... <tr id="variant_100329" class="variantRow">
... '''
>>> [filter(str.isdigit, line) for line in lines.splitlines()]
['34980', '100329']
</code></pre>
<hr/>
<p><strong>使用<a href="http://lxml.de" rel="nofollow">^{<cd3>}</a>更新</strong>:</p>
<pre><code>import lxml.html
html_string = '''
<tr id="product_34980" class="even">
<tr id="variant_100329" class="variantRow">
'''
root = lxml.html.fromstring(html_string)
for tr in root.cssselect('tr.even, tr.variantRow'):
print(tr.get('id')) # => product_34980
print(tr.get('id').rsplit('_', 1)[-1]) # => 34980
</code></pre>