HTML taglist creator 08-28-2014, 02:22 PM
#1
Hello [username], this is a simple web crawling related function. I find it very handy when it comes to html code manipulations so I decided to share it with you. I coded and tested this on python2.7 it maybe works on other versions too, but make sure the version is able to install and use BeautifulSoup4 module properly.
Requirements : python2.7, bs4(BeautifulSoup4)
Code :
Sample Run :
Results :
![[Image: O7HnMOH.png]](http://i.imgur.com/O7HnMOH.png)
![[Image: aQRfTSb.png]](http://i.imgur.com/aQRfTSb.png)
Replies/thoughts will be appreciated as always.
I hope you will find it interesting. Kappa
Thanks you for you time.
Sincerely,
L0aD1nG
EDIT : Forgotted the HEADERS constant added now. If it didn't work that was the problem, my bad.
Requirements : python2.7, bs4(BeautifulSoup4)
Code :
Code:
## Importing
from bs4 import BeautifulSoup
from urllib2 import Request, urlopen
## Predifined Header Constant
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
def tag_list_create(url, tag):
"""This functions will create and return
a list of all the tags from the given url's
html code each list item as a str object.
"""
## Setting up the request
request = Request('http://' + url, headers=HEADERS)
## Opening the site
site_open = urlopen(request)
## Reading the html code
site = site_open.read()
## Setting the soup
soup = BeautifulSoup(site)
## Getting&converting the tags
taglist = map(str, soup.findAll(tag))
return taglistSample Run :
Code:
from pprint import pprint
pprint(tag_list_create('www.nytimes.com', 'p'))Results :
Spoiler: Double Image
![[Image: O7HnMOH.png]](http://i.imgur.com/O7HnMOH.png)
![[Image: aQRfTSb.png]](http://i.imgur.com/aQRfTSb.png)
Replies/thoughts will be appreciated as always.
I hope you will find it interesting. Kappa
Thanks you for you time.
Sincerely,
L0aD1nG
EDIT : Forgotted the HEADERS constant added now. If it didn't work that was the problem, my bad.
(This post was last modified: 08-28-2014, 07:29 PM by krozz.)
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.
![[Image: qJweLN6.jpg]](http://i.imgur.com/qJweLN6.jpg)


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)