在RDF catalog from from Project Gutenberg中找不到全文。不過,它確實包含了多種格式的文本的URL。一旦您下載了catalog zip file並解壓縮之後,下面介紹如何從特定的RDF文件獲取HTML書籍網址。
filename = 'cache/epub/78/pg78.rdf'
from lxml import etree
rdf = open(filename).read()
tree = etree.fromstring(rdf)
resource_tag = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource'
hasFormat_tag = './/{http://purl.org/dc/terms/}hasFormat'
resources = [el.attrib[resource_tag] for el in tree.findall(hasFormat_tag)]
urls = [url for url in resources if url.endswith('htm')]
// urls[0] is 'http://www.gutenberg.org/files/78/78-h/78-h.htm'
一旦你有了你想要的書的HTML版本的URL,以下是如何獲取文本。
import requests
from lxml import etree
response = requests.get(urls[0])
html = etree.HTML(response.text)
text = '\n'.join([el.text for el in html.findall('.//p')])
text
現在包含的Tarzan全文,減去Project Gutenberg的元數據,目錄和章節標題。
>>> text[:100]
u'\r\nI had this story from one who had no business to tell it to me, or to\r\nany other. I may credit th'
請注意,古登堡書籍之間存在不一致,因此您的結果可能會有所不同。
Project Gutenberg在RDF中提供他們的目錄,而不是實際的書本文本,所以這不會解決您的問題 – RobV
@RobV我認爲用戶的觀點是三元' dcterms:hasFormat 提供文本的URL。不過,我同意,如果禁止自動訪問,只需擁有一個URL不會有幫助,因爲訪問它仍然是自動訪問。 –
在Google上搜索什麼並沒有多大幫助?從[Google搜索rdf](https://www.google.com/search?q=rdf)開始的幾個點擊都是關於RDF的,它是如何使用的,它看起來像什麼等。 –