Python Beautiful Soup-`findAll`没有捕获SVG中的所有标记(`ElementTree`有)
我试图通过修改一个描述美国所有县的地图来生成一张choropleth地图。基本方法由以下内容捕获。由于SVG基本上只是XML,因此该方法利用了解析器 问题是,解析器不会捕获SVG文件中的所有Python Beautiful Soup-`findAll`没有捕获SVG中的所有标记(`ElementTree`有),python,svg,beautifulsoup,elementtree,choropleth,Python,Svg,Beautifulsoup,Elementtree,Choropleth,我试图通过修改一个描述美国所有县的地图来生成一张choropleth地图。基本方法由以下内容捕获。由于SVG基本上只是XML,因此该方法利用了解析器 问题是,解析器不会捕获SVG文件中的所有path元素。以下仅捕获了149条路径(超过3000条): 然而,我知道,从物理检查和方法通过以下例程捕获3143条路径的事实来看,还有更多的情况存在: #Parse SVG tree = ET.parse(shp_dir+'USA_Counties_with_FIPS_and_names.svg') #C
path
元素。以下仅捕获了149条路径(超过3000条):
然而,我知道,从物理检查和方法通过以下例程捕获3143条路径的事实来看,还有更多的情况存在:
#Parse SVG
tree = ET.parse(shp_dir+'USA_Counties_with_FIPS_and_names.svg')
#Capture element
root = tree.getroot()
#Compile list of IDs from file
ids=[]
for child in root:
if 'path' in child.tag:
ids.append(child.attrib['id'])
len(ids)
我还没有弄清楚如何从ElementTree
对象中以一种不完全混乱的方式进行编写
#Define style template string
style='font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;'+\
'stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;'+\
'stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'
#For each path...
for child in root:
#...if it is a path....
if 'path' in child.tag:
try:
#...update the style to the new string with a county-specific color...
child.attrib['style']=style+col_map[child.attrib['id']]
except:
#...if it's not a county we have in the ACS, leave it alone
child.attrib['style']=style+'#d0d0d0'+'\n'
#Write modified SVG to disk
tree.write(shp_dir+'mhv_by_cty.svg')
上面的修改/写入例程产生了这样一个怪物:
我的主要问题是:为什么BeautifulSoup无法捕获所有的
路径
标记?其次,为什么用ElementTree
对象修改的图像会有所有的课外活动?如果您有任何建议,我们将不胜感激。您需要执行以下操作:
- 升级至:
- 将其导入为:
from bs4 import BeautifulSoup
- 安装最新的
模块:lxml
pip install lxml -U
- 明确指定
作为解析器:lxml
soup = BeautifulSoup(svg, 'lxml')
亚历克斯的回答对你的第一个问题是正确的。关于你的第二个问题: 为什么用ElementTree对象修改的图像会有所有的课外活动?” 答案很简单-并不是每个
元素都绘制一个县。具体来说,有两个元素应该被删除,一个是id=“State\u line”
,另一个是id=“separator”
。您没有提供颜色数据集,所以我只使用了一个随机十六进制颜色生成器(改编自)对于每个县,然后用于解析.svg
的XML,并迭代每个
元素,跳过上面提到的元素:
from lxml import etree as ET
import random
def random_color():
r = lambda: random.randint(0,255)
return '#%02X%02X%02X' % (r(),r(),r())
new_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'
tree = ET.parse('USA_Counties_with_FIPS_and_names.svg')
root = tree.getroot()
for child in root:
if 'path' in child.tag and child.attrib['id'] not in ["separator", "State_Lines"]:
child.attrib['style'] = new_style + random_color()
tree.write('counties_new.svg')
产生了这样一幅漂亮的图像:
使用BeautifulSoup 4.3.2,我运行了
svg_-soup=BeautifulSoup(svg);path=svg_-soup.find_-all('path');len(path)
,输出了3143条。也许你需要升级bs4
,效果非常好。这肯定是一个疏忽,我被与任何特定边界不对应的模式所抛弃。
soup = BeautifulSoup(svg, 'lxml')
>>> from bs4 import BeautifulSoup
>>>
>>> svg = open('USA_Counties_with_FIPS_and_names.svg','r').read()
>>> soup = BeautifulSoup(svg, 'lxml')
>>> paths = soup.findAll('path')
>>> len(paths)
3143
from lxml import etree as ET
import random
def random_color():
r = lambda: random.randint(0,255)
return '#%02X%02X%02X' % (r(),r(),r())
new_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'
tree = ET.parse('USA_Counties_with_FIPS_and_names.svg')
root = tree.getroot()
for child in root:
if 'path' in child.tag and child.attrib['id'] not in ["separator", "State_Lines"]:
child.attrib['style'] = new_style + random_color()
tree.write('counties_new.svg')