我有一个充满 html 文件的文件夹。我试图选择正确的 html 标签,以便正确打印引文,并且我需要的输出只是出版号和标题。到目前为止,我在 SO 中各个帖子的帮助下做到了这一点
with open(filename, 'r',encoding='utf-8') as f:# start loop to read HTML files in folder
patent = f.read()
#print(filename)
soup = BeautifulSoup(patent, 'html.parser')
x=soup.select('tr[itemprop="backwardReferencesOrig"]')
backorigdf= pd.read_html(str(x))
print(backorigdf.loc[: , ['Publication number', 'Title']
但我收到错误消息 ValueError: Notablesfound。我希望以 pandas 数据帧格式输出多个 HTML 文件引用,以便我更容易分析数据。有人可以告诉我我做错了什么吗?这是 HTML 文件的链接https://patents.google.com/patent/US4458945?oq=US4458945A https://patents.google.com/patent/US4458945?oq=US4458945A。该文件以 HTML 文件形式保存在我的计算机上,我不想从 URL 中读取。我想用代码从 HTML 文档中获取。
了解预期结果的总数将有所帮助。在下面,我使用 :contains 来定位引文 h2 元素,然后移动到相邻的表,从而检索 25 个唯一结果
from bs4 import BeautifulSoup as bs
import requests
import pandas as pd
r = requests.get('https://patents.google.com/patent/US4458945?oq=US4458945A')
soup = bs(r.content, 'lxml')
df = pd.concat([pd.read_html(str(t.find_next('table')))[0]
for t in soup.select('h2:contains("Citations", "Family Cites")')])
df.drop_duplicates(inplace=True)
df.sort_values(by=['Priority date'], inplace=True)
df.reset_index(drop=True, inplace=True)
print(df)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)