简体   繁体   中英

Beautiful soup - how to extract a string from an object

I am learning Beautiful soup. I have succeeded in tracking down the html lines that I need. My next step is to extract an Id value from those lines.

The code to find the lines looks like this:

object = soup_station.find('img',{'src': re.compile("^Controls")})

If I now print object I will get this, for example:

<img src="Controls/RiverLevels/ChartImage.jpg?Id=471&amp;ChartType=Histogram" id="StationDetails_Chart1_chartImage" alt="Current river level" />

The part I want to extract in the line above is the "471" after Id= .

I tried using re.search on object but it seems that object is not text.

Any help would be much appreciated!

You can adapt the following:

s = '<img src="Controls/RiverLevels/ChartImage.jpg?Id=471&amp;ChartType=Histogram" id="StationDetails_Chart1_chartImage" alt="Current river level" />'

from bs4 import BeautifulSoup
import re
from urlparse import urlsplit, parse_qs


soup = BeautifulSoup(s)
# find the node with a src starting with Controls
node = soup.find('img',{'src': re.compile("^Controls")})
# Break up the url in the src attribute
url_split = urlsplit(node['src'])
# Parse out the query parameter from the url
qs = parse_qs(url_split.query)
# Display the value for `Id`
print qs['Id'][0]

You want to make sure that you are performing the regex search on the object's source. You can give this a try:

import re
ele = soup_station.find('img')
src = ele['src']

match = re.search(r'\?Id=(\d+)', src)
ele_id = match.group(1)

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