簡體   English   中英

為什么我不能在這段代碼中使用 findall() 方法以及解決方案是什么

[英]why I can't use findall()method in this code and what is the solution

在下面的代碼中,它可以正常工作,直到findall 我該如何解決這個問題? 此代碼的主要目的是接收存儲在此站點中的數據,此鏈接包含 xml 數據,但我試圖從 beautifulsoup 獲取數據並將其視為 html 文件。 我知道另一種解決方案,但我需要嘗試一下。

回溯錯誤warnings.warn(回溯(最近一次調用最后一次):
文件“D:\program\venv\try.py”,第 10 行,在 datas=dp.findall('comment') 文件“C:\Users\Abdullah\AppData\Local\Programs\Python\Python310\lib\xml \etree\ElementTree.py",第 669 行,在 findall 中返回 self._root.findall(path, namespaces)

TypeError:“NoneType”對象不可調用

from bs4 import BeautifulSoup
import urllib.request,urllib.error,urllib.parse
import re;import lxml
import xml.etree.ElementTree as ET
count =0
html=urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_1591221.xml').read()
url=BeautifulSoup(html,'html.parser')
dp=ET.ElementTree(url)
datas=dp.findall('comment')
import requests
from bs4 import BeautifulSoup

url='http://py4e-data.dr-chuck.net/comments_1591221.xml'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')

comments = soup.select('comment')
print(comments)

生成一個包含所有comment標簽的列表:

[<comment>
<name>Ajay</name>
<count>99</count>
</comment>, <comment>
<name>Sheonagh</name>
<count>99</count>
</comment>,...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM