PyQuery 不会 return 页面上的元素

PyQuery won't return elements on a page

我已经设置了 Python 脚本来使用 PyQuery 打开此网页。

import requests
from pyquery import PyQuery

url = "http://www.floridaleagueofcities.com/widgets/cityofficials?CityID=101"
page = requests.get(url)
pqPage = PyQuery(page.content)

但是 pqPage("li") return 只是一个空白列表,[]。同时,pqPage.text() 显示页面 HTML 的文本,其中包括 li 个元素。

为什么代码 return 不是 li 元素的列表?我如何让它做到这一点?

似乎 PyQuery 使用此页面有问题 - 可能是因为它是 xhtml 页面。或者可能是因为它使用命名空间 xmlns="http://www.w3.org/1999/xhtml"

当我使用

pqPage.css('li')

然后我得到

[<{http://www.w3.org/1999/xhtml}html#sfFrontendHtml>]

在元素中显示 {http://www.w3.org/1999/xhtml} - 它是 namespace。某些模块在使用命名空间的 HTML 方面存在问题。


我使用Beautifulsoup

获取它没问题
import requests
from bs4 import BeautifulSoup as BS

url = "http://www.floridaleagueofcities.com/widgets/cityofficials?CityID=101"
page = requests.get(url)

soup = BS(page.text, 'html.parser')
for item in soup.find_all('li'):
    print(item.text)

编辑: 在 Google 中挖掘后我发现在 PyQuery() 中使用 parser="html" 我可以得到 li.

import requests
from pyquery import PyQuery

url = "http://www.floridaleagueofcities.com/widgets/cityofficials?CityID=101"
page = requests.get(url)

pqPage = PyQuery(page.text, parser="html")
for item in pqPage('li p'):
    print(item.text)