Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python美化组提取文本_Python_Beautifulsoup - Fatal编程技术网

python美化组提取文本

python美化组提取文本,python,beautifulsoup,Python,Beautifulsoup,我正试图从下面的html源代码中提取Manufacturer#和PAW11295,结果卡住了。感谢您的建议 soupTest.find("div",id = "AddnInfo") Out[121]: <div id="AddnInfo"> <h3>Additional Info</h3> <p> <p class="sknText"><label>“R”Web#:</label> <span class

我正试图从下面的html源代码中提取
Manufacturer#
PAW11295
,结果卡住了。感谢您的建议

soupTest.find("div",id = "AddnInfo")
Out[121]: 
<div id="AddnInfo">
<h3>Additional Info</h3>
<p>
<p class="sknText"><label>“R”Web#:</label> <span class="value">215904</span>   </p>
<p class="skuText"><label>SKU:</label> <span class="value">B7958C02</span>    </p>
<p class="upc"><label>UPC/EAN/ISBN:</label> <span class="value">092317112958</span></p>
<p><label>Manufacturer #:</label> PAW11295</p>
<p><label>Product Weight:</label>2.2 pounds</p>
<p><label>Product Dimensions (in inches):</label>12.7 x 10.1 x 5.4</p>
</p>
</div>
soupTest.find(“div”,id=“AddnInfo”)
出[121]:
附加信息

“R”网站:215904

SKU:B7958C02

upc/EAN/ISBN:092317112958

制造商#:PAW11295

产品重量:2.2磅

产品尺寸(英寸):12.7 x 10.1 x 5.4


提前谢谢。

我想你想要这样的东西。 首先选择外部P标记。然后选择所有内部P标记。然后引用所需的单个P标记,在本例中是第四个

infoDiv = soupTest.find("div",id = "AddnInfo")
outerPs = infoDiv.p  # isolate the outer <P>
innerPs = outerPs.find_all('p')  # returns a list of the inner <P>s
manufacturer_number = innerPs[3].string  # you will have to trim the <label>
manufacturer_code = innerPs[3].label.string  # will need trimming
infoDiv=soupTest.find(“div”,id=“AddnInfo”)
outerPs=infoDiv.p#隔离外部
innerPs=outerPs.find_all('p')#返回内部s的列表
制造商编号=innerPs[3]。字符串#您必须修剪
制造商代码=innerPs[3]。label.string需要修剪

我想你想要这样的东西。 首先选择外部P标记。然后选择所有内部P标记。然后引用所需的单个P标记,在本例中是第四个

infoDiv = soupTest.find("div",id = "AddnInfo")
outerPs = infoDiv.p  # isolate the outer <P>
innerPs = outerPs.find_all('p')  # returns a list of the inner <P>s
manufacturer_number = innerPs[3].string  # you will have to trim the <label>
manufacturer_code = innerPs[3].label.string  # will need trimming
infoDiv=soupTest.find(“div”,id=“AddnInfo”)
outerPs=infoDiv.p#隔离外部
innerPs=outerPs.find_all('p')#返回内部s的列表
制造商编号=innerPs[3]。字符串#您必须修剪
制造商代码=innerPs[3]。label.string需要修剪

以下方法应该有效。它获取第5个
元素并获取
文本。然后,它将删除该标记并显示整个
标记的剥离文本:

from bs4 import BeautifulSoup

html = """
<div id="AddnInfo">
<h3>Additional Info</h3>
<p>
<p class="sknText"><label>“R”Web#:</label> <span class="value">215904</span>   </p>
<p class="skuText"><label>SKU:</label> <span class="value">B7958C02</span>    </p>
<p class="upc"><label>UPC/EAN/ISBN:</label> <span class="value">092317112958</span></p>
<p><label>Manufacturer #:</label> PAW11295</p>
<p><label>Product Weight:</label>2.2 pounds</p>
<p><label>Product Dimensions (in inches):</label>12.7 x 10.1 x 5.4</p>
</p>
</div>
"""

soup = BeautifulSoup(html)
div = soup.find('div', {'id':'AddnInfo'})
p = div.find_all('p')[4]
label = p.find('label')
manufacturer = label.text
label.extract()
id = p.get_text(strip=True)
print manufacturer
print id

以下方法应该有效。它获取第5个
元素并获取
文本。然后,它将删除该标记并显示整个
标记的剥离文本:

from bs4 import BeautifulSoup

html = """
<div id="AddnInfo">
<h3>Additional Info</h3>
<p>
<p class="sknText"><label>“R”Web#:</label> <span class="value">215904</span>   </p>
<p class="skuText"><label>SKU:</label> <span class="value">B7958C02</span>    </p>
<p class="upc"><label>UPC/EAN/ISBN:</label> <span class="value">092317112958</span></p>
<p><label>Manufacturer #:</label> PAW11295</p>
<p><label>Product Weight:</label>2.2 pounds</p>
<p><label>Product Dimensions (in inches):</label>12.7 x 10.1 x 5.4</p>
</p>
</div>
"""

soup = BeautifulSoup(html)
div = soup.find('div', {'id':'AddnInfo'})
p = div.find_all('p')[4]
label = p.find('label')
manufacturer = label.text
label.extract()
id = p.get_text(strip=True)
print manufacturer
print id

好吧,你觉得怎么样?你在哪里被卡住了?好吧,你觉得怎么样?“你被卡在哪里了?谢谢,”马丁说。它起作用了。我很好奇为什么在
label.extract()
之后,
p
变成了
PAW11295

,并且无法将它带回
制造商:PAW11295

?我查看了office文档,但仍然感到困惑。
extract()
删除标记,就像删除命令一样。如果你需要保留html,你可以在做这件事之前复制一份。明白了。再次感谢!谢谢,马丁。它起作用了。我很好奇为什么在
label.extract()
之后,
p
变成了
PAW11295

,并且无法将它带回
制造商:PAW11295

?我查看了office文档,但仍然感到困惑。
extract()
删除标记,就像删除命令一样。如果需要保留html,可以在执行此操作之前制作一个副本。明白了。再次感谢!