-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbs4_sample2.py
More file actions
53 lines (32 loc) · 1.77 KB
/
Copy pathbs4_sample2.py
File metadata and controls
53 lines (32 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from pprint import pprint
import re
from bs4 import BeautifulSoup
html_content = open('bs_sample.html') # http://dl.dropbox.com/u/49962071/blog/python/resource/bs_sample.html
soup = BeautifulSoup(html_content) # making soap
for tag in soup.find_all(re.compile("^p")): # find all tag start with p
print tag.name
for tag in soup.find_all(re.compile("t")): # find all tag contains t
print tag.name
for tag in soup.find_all(True): # find all tag
print tag.name
pprint(soup.find_all('a')) # find all a tag
print 20*"++"
pprint(soup.find_all(["a", "b"])) # find multiple tag
def has_class_but_no_id(tag):
return tag.has_key('class') and not tag.has_key('id')
pprint(soup.find_all(has_class_but_no_id)) # pass a function to find_all
pprint(soup.find_all(text=re.compile("sisters"))) # find all tag content contains key 'sisters'
print 20*"++"
pprint(soup.find_all(href=re.compile("my_url"))) # all links contains key "my_url"
pprint(soup.find_all(id=True)) # all links has id
pprint(soup.find_all(class_=True)) # all links has class
def has_six_characters(css_class):
return css_class is not None and len(css_class) == 7
pprint(soup.find_all(class_=has_six_characters)) # find all class name contains 7 characters
pprint(soup.find_all("a", "sister")) # find all a tag have class named 'sister'
pprint(soup.find_all("a", re.compile("sister"))) # find all a tag have class named contains 'sister'
print 20*"++"
pprint(soup.find_all(href=re.compile("elsie"), id='link1')) # url name contains elsie and have id = link1
pprint(soup.find_all(attrs={'href' : re.compile("elsie"), 'id': 'link1'})) # url name contains elsie and have id = link1
pprint(soup.find_all("a", limit=2)) # use limit on findall
pprint(soup.html.find_all("title", recursive=True)) # use recursive on findall