【问题标题】:I need to create a new string, with the contents being the title of a website我需要创建一个新字符串,内容是网站的标题
【发布时间】:2018-05-13 01:40:11
【问题描述】:

我虽然像 (1) 这样的东西可以工作,但它会引发错误。有什么想法或提示吗?

(1)

versionPreCheck = lxml.html.parse("URL")
versionCheck = versionPreCheck.find(".//title").text

LatestVersion = (versionCheck.read())

错误:

Traceback (most recent call last):
  File "python", line 132, in <module>
  File "src/lxml/etree.pyx", line 3426, in lxml.etree.parse
  File "src/lxml/parser.pxi", line 1839, in lxml.etree._parseDocument
  File "src/lxml/parser.pxi", line 1865, in lxml.etree._parseDocumentFromURL
  File "src/lxml/parser.pxi", line 1769, in lxml.etree._parseDocFromFile
  File "src/lxml/parser.pxi", line 1162, in lxml.etree._BaseParser._parseDocFromFile
  File "src/lxml/parser.pxi", line 600, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 710, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 637, in lxml.etree._raiseParseError
OSError: Error reading file 'bazorkversion--grify.repl.co': failed to load external entity "bazorkversion--grify.repl.co"

这是标题:

https://bazorkversion--grify.repl.co/ 标题是字符串“PreAlpha 3” (它出现在浏览器选项卡的顶部,网站图标旁边)

【问题讨论】:

  • “标题”是什么意思?网址的标题?有没有一个叫title的标签?也许提供一个有效的 URL 作为示例
  • 将我的错误添加到正文中

标签: python web


【解决方案1】:

You aren't the only one receiving this error,也可能是lxml的故障。

相反,也许可以尝试使用另一个网络抓取模块,例如 BeautifulSoup,以及 requests 模块来接收来自 URL 的请求:

>>> import requests
>>> from bs4 import BeautifulSoup as BS
>>> r = requests.get('https://bazorkversion--grify.repl.co/')
>>> soup = BS(r.text, 'lxml')
>>> soup.title.text
'PreAlpha 3'

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 2023-03-16
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多