简体   繁体   English

通过python解析来自网站的信息

[英]Parse information from a website through python

I'm trying to read a number from a website into a variable.我正在尝试将一个网站上的数字读入一个变量中。 The source code where the number is looks like this:编号所在的源代码如下所示:

<tr bgcolor="#ccffff"><td>N_300_0</td><td>5918.720</td></tr>

The website will always say N_300_0 but the number will change.该网站将始终显示 N_300_0,但数字会发生变化。

So far I have:到目前为止,我有:

link = urllib2.urlopen("http://www.example.com").read()
matches = re.findall('N_300_0', link);
number = ....

How do I get the number into the variable?如何将数字放入变量中?

If you are doing any serious or involved scraping, I would strongly agree that something like BeautifulSoup is a much better way to go.如果你正在做任何严肃的或涉及到的抓取,我强烈同意像BeautifulSoup这样的东西是一个更好的方法。

But to answer your question, you need to use grouping in python regex via parens to do the sort of capturing you want, eg但是要回答您的问题,您需要通过parens在python regex中使用分组来进行您想要的那种捕获,例如

numbers = re.findall('N_300_0</td><td>([-+]?\d*\.\d+|\d+)',s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM