Python Forum
Thread Rating:
  • 3 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web-scraping part-2
#5
I was thinking more of xpath method, something like an alternative to BeautifulSoup side by side

from lxml import etree
from bs4 import BeautifulSoup

html = '<html><head><title>foo</title></head><body><div class="other"></div><div class="name"><p>foo</p></div><div class="name"><ul><li>bar</li></ul></div></body></html>'

tree = etree.fromstring(html)
for elem in tree.xpath("//div[@class='name']"):
    print(etree.tostring(elem, pretty_print=True))
     
soup = BeautifulSoup(html, 'lxml')
for elem in soup.find_all('div', {'class':'name'}):
    print(elem.prettify())
Recommended Tutorials:
Reply


Messages In This Thread
Web-scraping part-2 - by snippsat - Oct-30-2016, 11:21 PM
RE: Web-scraping part-2 - by metulburr - Oct-31-2016, 12:56 AM
RE: Web-scraping part-2 - by metulburr - Oct-31-2016, 12:56 AM
RE: Web-scraping part-2 - by snippsat - Oct-31-2016, 01:17 AM
RE: Web-scraping part-2 - by metulburr - Jan-29-2017, 01:35 AM
RE: Web-scraping part-2 - by snippsat - Jan-29-2017, 03:49 PM
RE: Web-scraping part-2 - by snippsat - Jan-30-2017, 01:43 PM
RE: Web-scraping part-2 - by metulburr - Oct-21-2017, 12:10 AM
RE: Web-scraping part-2 - by snippsat - Apr-01-2018, 02:06 AM
RE: Web-scraping part-2 - by metulburr - Oct-15-2018, 11:59 PM
RE: Web-scraping part-2 - by snippsat - Oct-16-2018, 04:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Web-Scraping part-1 snippsat 2 34,901 Jun-08-2017, 10:55 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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