使用python提取html文件中的特定部分

2024-10-01 19:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何提取html文件的特定部分示例https://patents.google.com/patent/EP1208209A1/en?oq=medicinal+chemistry

到目前为止,我使用beautifulsoup获取html的文本版本,而不包含所有标记。但是我希望我的代码只读上面提到的文件的索赔部分


Tags: 文件https文本com示例htmlgoogleen
3条回答
filename= 'C:/Users/xyz/.ipynb_checkpoints/EP1208209A1.html'
html_file =open(filename, 'r', encoding='utf-8')
source_code = html_file.read() 
#print(source_code)
soup = BeautifulSoup(source_code)
print(soup.get_text())
#mydivs = soup.findAll("div", {"class": "flex flex-width style-scope patent-result"})
#div_with_claims = mydivs [1]
#print(div_with_claims)

据我所知,有两个div和^{cl1}$

soup = BeautifulSoup(sdata)
mydivs = soup.findAll("div", {"class": "flex flex-width style-scope patent-result"})
div_with_claims = mydivs [1]

这里有mate,我发现在这个站点中,索赔部分是一个带有自己Id的html,使事情变得更简单。我只是把这一部分整理好,给你一根绳子,你就可以玩了

import requests
from bs4 import BeautifulSoup
page = requests.get("https://patents.google.com/patent/EP1208209A1/en?oq=medicinal+chemistry")
soup = BeautifulSoup(page.content, 'html.parser')
claim_sect = soup.find_all('section', attrs={"itemprop":"claims"})
print('This is the raw content: \n') 
print(str(claim_sect)) 
print('This is the variable type: \n') 
print(str(type(claim_sect))) 
str_sect  =  claim_sect[0]

相关问题 更多 >

    热门问题