我知道还有其他问题,但是我无法到达那里的解释,请在下面提供我的代码。
我希望输出使字典像
dictionary
{
'[1.1]':'this is extracted text from a parent tag',
'[1.2]':'this is child tag text',
'[1.3]':'this is child tag text',
'[1.4]':'this is child tag text'
}
但是问题是我不仅在父标签中得到[1.1]
中的父标签加上子标签的文本。
我尝试了其他解决方案,但无法解决。请以简单的方式帮助别人。
我的代码在这里,
from bs4 import BeautifulSoup
import requests
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
})
URL = "https://patents.google.com/patent/US20120303322A1/en"
content = requests.get(URL, headers=headers)
soup = BeautifulSoup(content.text,'html.parser')
independent_claim_tag = soup.find('div',{'class':'claim'})
claimdictionary = {}
# While loop to get all the independent claims tag works perfectly!!
while(independent_claim_tag):
base = independent_claim_tag.find("div", {"class":"claim"})['num'].lstrip('0')
print(independent_claim_tag.prettify())
print('-------')
elementTags = independent_claim_tag.find_all('div', {'class':'claim-text'})
i = 1
for tag in elementTags:
key = "[ "+str(base)+"."+str(i)+" ] "
######################
# some code need to be here to get only parent tag text for [1.1]
value = tag.get_text()
######################
claimdictionary[key.strip()] = value.strip()
print("[ "+str(base)+"."+str(i)+" ] "+tag.get_text())
i = i + 1
print('-------')
##################
##################
print("Number of claim Element: "+str(len(independent_claim_tag.find_all('div',{'class':'claim-text'}))))
print("---- Next Sibling")
independent_claim_tag = independent_claim_tag.find_next_sibling('div',{'class':'claim'})
print(claimdictionary)
我需要提取的HTML标记
<div class="claim">
<div class="claim" id="CLM-00001" num="00001">
<div class="claim-text">
<b>
1
</b>
. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:
<div class="claim-text">
receive data corresponding to a computing node;
</div>
<div class="claim-text">
identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node; and
</div>
<div class="claim-text">
determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
</div>
</div>
</div>
</div>
Number of claim Element: 4
此处索赔元素4的数字表示
{
'[1.1]' : '1. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:',
'[1.2]' : 'receive data corresponding to a computing node;',
'[1.3]' : 'identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node; and',
'[1.4]' : 'determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.'
}
更新:这是经过一些更新后的输出
<div class="claim">
<div class="claim" id="CLM-00001" num="00001">
<div class="claim-text">
<b>
1
</b>
. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:
<div class="claim-text">
receive data corresponding to a computing node;
</div>
<div class="claim-text">
identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node; and
</div>
<div class="claim-text">
determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
</div>
</div>
</div>
</div>
-------
[ 1.1 ] 1. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:
receive data corresponding to a computing node; identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node; and determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
[ 1.2 ] receive data corresponding to a computing node;
[ 1.3 ] identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node; and
[ 1.4 ] determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
-------
Number of claim Element: 4
---- Next Sibling
<div class="claim">
<div class="claim" id="CLM-00008" num="00008">
<div class="claim-text">
<b>
8
</b>
. A system comprising:
<div class="claim-text">
a processor; and
</div>
<div class="claim-text">
a computer readable storage medium including a set of instructions which, if executed by the processor, cause the system to,
<div class="claim-text">
receive data corresponding to a computing node,
</div>
<div class="claim-text">
identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node, and
</div>
<div class="claim-text">
determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
</div>
</div>
</div>
</div>
</div>
-------
[ 8.1 ] 8. A system comprising:
a processor; and a computer readable storage medium including a set of instructions which, if executed by the processor, cause the system to,
receive data corresponding to a computing node,
identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node, and
determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
[ 8.2 ] a processor; and
[ 8.3 ] a computer readable storage medium including a set of instructions which, if executed by the processor, cause the system to,
receive data corresponding to a computing node,
identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node, and
determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
[ 8.4 ] receive data corresponding to a computing node,
[ 8.5 ] identify a processor usage, a memory usage and an input/output usage based at least in part on the data corresponding to the computing node, and
[ 8.6 ] determine a compute usage value for the computing node based at least in part on the processor usage, the memory usage and the input/output usage.
-------
Number of claim Element: 6
---- Next Sibling
<div class="claim">
<div class="claim" id="CLM-00015" num="00015">
<div class="claim-text">
<b>
15
</b>
. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:
<div class="claim-text">
collect data corresponding to a computing node, wherein the data is to be associated with a processor usage, a memory usage and an input/output usage; and
</div>
<div class="claim-text">
send the data to a compute usage calculation node.
</div>
</div>
</div>
</div>
-------
[ 15.1 ] 15. A computer readable storage medium comprising a set of instructions which, if executed by a processor, cause a computer to:
collect data corresponding to a computing node, wherein the data is to be associated with a processor usage, a memory usage and an input/output usage; and send the data to a compute usage calculation node.
[ 15.2 ] collect data corresponding to a computing node, wherein the data is to be associated with a processor usage, a memory usage and an input/output usage; and
[ 15.3 ] send the data to a compute usage calculation node.
-------
Number of claim Element: 3
---- Next Sibling
答案 0 :(得分:1)
将父标签中的子元素添加到字典中时,您可以extract()
将其添加到字典中:
from bs4 import BeautifulSoup
import requests
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
})
URL = "https://patents.google.com/patent/US20120303322A1/en"
content = requests.get(URL, headers=headers)
soup = BeautifulSoup(content.text,'html.parser')
independent_claim_tag = soup.find('div',{'class':'claim'})
claimdictionary = {}
# While loop to get all the independent claims tag works perfectly!!
while(independent_claim_tag):
base = independent_claim_tag.find("div", {"class":"claim"})['num'].lstrip('0')
print(independent_claim_tag.prettify())
print('-------')
elementTags = independent_claim_tag.find_all('div', {'class':'claim-text'})
i = 1
for tag in elementTags:
key = "[ "+str(base)+"."+str(i)+" ] "
if i == 1:
#parent
for subtag in tag.find_all('div',{'class':'claim-text'}):
subtag.extract()
value = tag.get_text()
else:
# child
value = tag.get_text()
claimdictionary[key.strip()] = value.strip()
print("[ "+str(base)+"."+str(i)+" ] "+tag.get_text())
i = i + 1
print('-------')
##################
# some code need to be here to process parent tag text from the child tag text
##################
print("Number of claim Element: "+str(len(independent_claim_tag.find_all('div',{'class':'claim-text'}))))
print("---- Next Sibling")
independent_claim_tag = independent_claim_tag.find_next_sibling('div',{'class':'claim'})
print(claimdictionary)
在这里您可以看到我检查了i
的值,如果i
为1,则删除了标记内的孩子。然后,我应用get_text()
方法。
编辑:
您可以删除else部分,也可以这样做:
if i == 1:
#parent
for subtag in tag.find_all('div',{'class':'claim-text'}):
subtag.extract()
value = tag.get_text()