s = '<img src="Controls/RiverLevels/ChartImage.jpg?Id=471&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]
您需要确保在对象的源上执行regex搜索。你可以试试看:
您可以调整以下内容:
相关问题 更多 >
编程相关推荐