Home > Software engineering >  why I can't use findall()method in this code and what is the solution
why I can't use findall()method in this code and what is the solution

Time:07-25

In the code below it works correct until findall. How can I solve this? The main purpose of this code to receive the data stored in this site and this link contain xml data but I am trying to get the data asif from beautifulsoup and treat it as html file. I know anther solution but I need to try this.

traceback error warnings.warn( Traceback (most recent call last):
File "D:\program\venv\try.py", line 10, in datas=dp.findall('comment') File "C:\Users\Abdullah\AppData\Local\Programs\Python\Python310\lib\xml\etree\ElementTree.py", line 669, in findall return self._root.findall(path, namespaces)

TypeError: 'NoneType' object is not callable

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')

CodePudding user response:

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)

Resulting in a list with all comment tags:

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