Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML parsing from URL
#3
My take on this is that you should drop urllib and ElementTree all together.
So what to use insted?
For reading url and all other HTTP work use Requests.
For parsing lxml and BeautifulSoup.
Have tutorial here.

A example,solve first task.
lxml:
from lxml import html
import requests

url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
response = requests.get(url)
tree = html.fromstring(response.content)
count = tree.xpath('//count')
total =  sum(int(i.text) for i in count)
print(f'The sum of all count is doc is: {total}')
Output:
The sum of all count in doc are: 2553
BeautifulSoup:
from bs4 import BeautifulSoup
import requests

url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
count = soup.find_all('count')
total =  sum(int(i.text) for i in count)
print(f'The sum of all count in doc are: {total}')
Output:
The sum of all count in doc are: 2553
Reply


Messages In This Thread
XML parsing from URL - by mightyn00b - Nov-21-2018, 03:28 AM
RE: XML parsing from URL - by stranac - Nov-21-2018, 07:37 AM
RE: XML parsing from URL - by mightyn00b - Nov-22-2018, 12:45 AM
RE: XML parsing from URL - by snippsat - Nov-21-2018, 04:34 PM
RE: XML parsing from URL - by mightyn00b - Nov-22-2018, 02:22 AM
RE: XML parsing from URL - by Larz60+ - Nov-22-2018, 02:59 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020