-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_bs4_OT.py
More file actions
71 lines (52 loc) · 1.79 KB
/
example_bs4_OT.py
File metadata and controls
71 lines (52 loc) · 1.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from bs4 import BeautifulSoup
import requests
helloworld = "<p>Hello World</p>"
soup_string = BeautifulSoup(helloworld, "html.parser")
print(soup_string)
#on récupère le contenu d'une page que l'on parse
url = "https://www.barnesandnoble.com/b/barnes-noble-classics/_/N-rqv"
response = requests.get(url)
soup_page = BeautifulSoup(response.content,"html.parser")
#print(soup_page)
#with open("foo.html") as foo_file:
# soup = BeautifulSoup(foo_file, "html.parser")
#print(soup)
html_atag = """<html><body><p>Test html a tag example</p>
<a href="http://www.test.com">Home</a>
<a href="http;//www.test.com/books">Books</a>
</body>
</html>"""
soup = BeautifulSoup(html_atag,"html.parser")
atag = soup.findAll('a')
for link in atag:
print(link.string)
print(atag)
html_identical = """<p class="identical">
Example of p tag with class identical
</p>
<div class="identical">
Example of div tag with class identical
</div>"""
soup = BeautifulSoup(html_identical, "html.parser")
identical_div = soup.find("div", class_="identical")
print(identical_div)
with open("ecologicalpyramid.html") as ecological_pyramid:
soup = BeautifulSoup(ecological_pyramid, "html.parser")
producer_entries = soup.find("ul")
print("entries: ",producer_entries.li.div.string)
t = soup.findAll("ul")
for v in t:
print(v.li.div.string)
tag_li = soup.find("li")
print(type(tag_li))
search = soup.find(text = 'fox')
print(search)
css_class = soup.find(attrs={'class': "primaryconsumerlist"})
print("CSS :",css_class)
def is_secondary_consumers(tag):
return tag.has_attr("id") and tag.get("id") == "secondaryconsumers"
secondary_consumers = soup.find(is_secondary_consumers)
print("test \n",secondary_consumers)
for v in secondary_consumers.find_all('div'):
print("V:",v)
if v != "" : print("Secondary consumers: ",v.string)