Hands-On Web Scraping with Python by Anish Chapagain

Hands-On Web Scraping with Python by Anish Chapagain

Author:Anish Chapagain
Language: eng
Format: epub, mobi
Tags: COM018000 - COMPUTERS / Data Processing, COM004000 - COMPUTERS / Intelligence (AI) and Semantics, COM021030 - COMPUTERS / Databases / Data Mining
Publisher: Packt Publishing
Published: 2019-06-14T06:10:57+00:00


print(list(soup.find('p','story').contents))

['Once upon a time there were three little sisters; and their names were\n', <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, ',\n', <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, ' and\n', <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>, ';\nand they lived at the bottom of a well.']

print(list(soup.find('p','story').descendants))

['Once upon a time there were three little sisters; and their names were\n', <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 'Elsie', ',\n', <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, 'Lacie', ' and\n', <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>, 'Tillie', ';\nand they lived at the bottom of a well.']

Selected children and descendants tag names can be obtained using the name attribute. Parsed strings and the \n function (newline) are returned as None, which can be filtered out, as in the following code:

#using List Comprehension Technique

print([a.name for a in soup.find('p','story').children])

[None, 'a', None, 'a', None, 'a', None]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.