简体   繁体   中英

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.

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.

But to answer your question, you need to use grouping in python regex via parens to do the sort of capturing you want, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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