问题描述
我有完整的古腾堡计划英语库,它是按字母顺序排列的 csv 文件,其中的列是 id,title,text
。这里 id
的格式为 /ebooks/15809
。然后我使用 Wikipedia python 包。我可以使用该包获取页面的全文和许多其他详细信息。
这是古腾堡的前 10 本书 -
['A Apple Pie','A Apple Pie and Other Nursery Tales','Aaron in the Wildwoods','Aaron Rodd',"Aaron's Rod",'Aaron the Jew: A Novel','Aaron Trow','Abaft the Funnel','Abandoned','The Abandoned Country; or']
现在,当我运行 pg = wikipedia.page('A Apple Pie')
时,我得到的是 Apple Pie、沙漠而不是书的结果。显然,API 的工作原理是当我们调用 wikipedia.page('xxxx')
时,它执行 wikipedia.search('xxxx')
返回搜索结果列表并返回第一个结果的 wiki 页面,在这种情况下是 -
>>> wikipedia.search('A Apple Pie')
['Apple pie','Pie','Apple Pie ABC','American Pie (film)','Sam Apple Pie',"Mom's Apple Pie",'Apple Pie Hill','Pie à la Mode','Apple crisp','Pieing']
>>>
因此我实际上需要清单上的第三本书。我想出的一种方法是查看古腾堡和维基百科中每个条目的类别。
至于古腾堡的第一本书,这些是它所属的类别-
s = 'https://www.gutenberg.org/ebooks/15809'
import requests
from bs4 import BeautifulSoup as bs
#page_url = base_url + alphabet
page = requests.get(s)
soup = bs(page.content,'html.parser')
bibrec_tbl = soup.find("table",{"class": "bibrec"})
for td in list(bibrec_tbl.findChildren('td')):
lowered = str(td).lower()
if 'itemprop' in lowered:
a = lowered[lowered.find('itemprop') + 10 :]
b = a[: a.find('"')]
print('itemprop','\t',b,td.text.strip())
elif 'property' in lowered:
a = lowered[lowered.find('property') + 10 :]
b = a[: a.find('"')]
print('property',td.text.strip())
itemprop creator Greenaway,Kate,1846-1901
itemprop headline A Apple Pie
property dcterms:subject Children's poetry
property dcterms:subject Nursery rhymes
property dcterms:subject Alphabet rhymes
property dcterms:subject Alphabet
property dcterms:type Text
itemprop datepublished May 10,2005
property dcterms:rights Public domain in the USA.
itemprop interactioncount 188 downloads in the last 30 days.
itemprop pricecurrency $0.00
对于第三个维基百科结果 -
pg = wikipedia.page('Apple Pie ABC')
print(pg.categories)
['Alphabet books','Articles with short description','British picture books','CS1 maint: discouraged parameter','Commons category link is on Wikidata',"English children's songs",'English folk songs','English nursery rhymes','Short description matches Wikidata',"Traditional children's songs"]
所以我能做的就是做两个类别之间的余弦相似度,并希望阈值足够接近以将标题与类别匹配。
有没有更好或更有效的方法来做到这一点?谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)