blob: 083395fb46ce803fd2679d875f15481564627f6e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""Diagnostic functions, mainly for use when doing tech support."""
Patrick Williamsc0f7c042017-02-23 20:41:17 -06002
3__license__ = "MIT"
4
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005import cProfile
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006from io import StringIO
7from html.parser import HTMLParser
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008import bs4
9from bs4 import BeautifulSoup, __version__
10from bs4.builder import builder_registry
11
12import os
13import pstats
14import random
15import tempfile
16import time
17import traceback
18import sys
19import cProfile
20
21def diagnose(data):
22 """Diagnostic suite for isolating common problems."""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060023 print("Diagnostic running on Beautiful Soup %s" % __version__)
24 print("Python version %s" % sys.version)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26 basic_parsers = ["html.parser", "html5lib", "lxml"]
27 for name in basic_parsers:
28 for builder in builder_registry.builders:
29 if name in builder.features:
30 break
31 else:
32 basic_parsers.remove(name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060033 print((
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 "I noticed that %s is not installed. Installing it may help." %
Patrick Williamsc0f7c042017-02-23 20:41:17 -060035 name))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
37 if 'lxml' in basic_parsers:
38 basic_parsers.append(["lxml", "xml"])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060039 try:
40 from lxml import etree
41 print("Found lxml version %s" % ".".join(map(str,etree.LXML_VERSION)))
42 except ImportError as e:
43 print (
44 "lxml is not installed or couldn't be imported.")
45
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
47 if 'html5lib' in basic_parsers:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048 try:
49 import html5lib
50 print("Found html5lib version %s" % html5lib.__version__)
51 except ImportError as e:
52 print (
53 "html5lib is not installed or couldn't be imported.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
55 if hasattr(data, 'read'):
56 data = data.read()
57 elif os.path.exists(data):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 print('"%s" looks like a filename. Reading data from the file.' % data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 data = open(data).read()
60 elif data.startswith("http:") or data.startswith("https:"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061 print('"%s" looks like a URL. Beautiful Soup is not an HTTP client.' % data)
62 print("You need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -060064 print()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 for parser in basic_parsers:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067 print("Trying to parse your markup with %s" % parser)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 success = False
69 try:
70 soup = BeautifulSoup(data, parser)
71 success = True
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 except Exception as e:
73 print("%s could not parse the markup." % parser)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 traceback.print_exc()
75 if success:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060076 print("Here's what %s did with the markup:" % parser)
77 print(soup.prettify())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 print("-" * 80)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
81def lxml_trace(data, html=True, **kwargs):
82 """Print out the lxml events that occur during parsing.
83
84 This lets you see how lxml parses a document when no Beautiful
85 Soup code is running.
86 """
87 from lxml import etree
88 for event, element in etree.iterparse(StringIO(data), html=html, **kwargs):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060089 print(("%s, %4s, %s" % (event, element.tag, element.text)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
91class AnnouncingParser(HTMLParser):
92 """Announces HTMLParser parse events, without doing anything else."""
93
94 def _p(self, s):
95 print(s)
96
97 def handle_starttag(self, name, attrs):
98 self._p("%s START" % name)
99
100 def handle_endtag(self, name):
101 self._p("%s END" % name)
102
103 def handle_data(self, data):
104 self._p("%s DATA" % data)
105
106 def handle_charref(self, name):
107 self._p("%s CHARREF" % name)
108
109 def handle_entityref(self, name):
110 self._p("%s ENTITYREF" % name)
111
112 def handle_comment(self, data):
113 self._p("%s COMMENT" % data)
114
115 def handle_decl(self, data):
116 self._p("%s DECL" % data)
117
118 def unknown_decl(self, data):
119 self._p("%s UNKNOWN-DECL" % data)
120
121 def handle_pi(self, data):
122 self._p("%s PI" % data)
123
124def htmlparser_trace(data):
125 """Print out the HTMLParser events that occur during parsing.
126
127 This lets you see how HTMLParser parses a document when no
128 Beautiful Soup code is running.
129 """
130 parser = AnnouncingParser()
131 parser.feed(data)
132
133_vowels = "aeiou"
134_consonants = "bcdfghjklmnpqrstvwxyz"
135
136def rword(length=5):
137 "Generate a random word-like string."
138 s = ''
139 for i in range(length):
140 if i % 2 == 0:
141 t = _consonants
142 else:
143 t = _vowels
144 s += random.choice(t)
145 return s
146
147def rsentence(length=4):
148 "Generate a random sentence-like string."
149 return " ".join(rword(random.randint(4,9)) for i in range(length))
150
151def rdoc(num_elements=1000):
152 """Randomly generate an invalid HTML document."""
153 tag_names = ['p', 'div', 'span', 'i', 'b', 'script', 'table']
154 elements = []
155 for i in range(num_elements):
156 choice = random.randint(0,3)
157 if choice == 0:
158 # New tag.
159 tag_name = random.choice(tag_names)
160 elements.append("<%s>" % tag_name)
161 elif choice == 1:
162 elements.append(rsentence(random.randint(1,4)))
163 elif choice == 2:
164 # Close a tag.
165 tag_name = random.choice(tag_names)
166 elements.append("</%s>" % tag_name)
167 return "<html>" + "\n".join(elements) + "</html>"
168
169def benchmark_parsers(num_elements=100000):
170 """Very basic head-to-head performance benchmark."""
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600171 print("Comparative parser benchmark on Beautiful Soup %s" % __version__)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 data = rdoc(num_elements)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173 print("Generated a large invalid HTML document (%d bytes)." % len(data))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174
175 for parser in ["lxml", ["lxml", "html"], "html5lib", "html.parser"]:
176 success = False
177 try:
178 a = time.time()
179 soup = BeautifulSoup(data, parser)
180 b = time.time()
181 success = True
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 except Exception as e:
183 print("%s could not parse the markup." % parser)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 traceback.print_exc()
185 if success:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600186 print("BS4+%s parsed the markup in %.2fs." % (parser, b-a))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187
188 from lxml import etree
189 a = time.time()
190 etree.HTML(data)
191 b = time.time()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600192 print("Raw lxml parsed the markup in %.2fs." % (b-a))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194 import html5lib
195 parser = html5lib.HTMLParser()
196 a = time.time()
197 parser.parse(data)
198 b = time.time()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600199 print("Raw html5lib parsed the markup in %.2fs." % (b-a))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200
201def profile(num_elements=100000, parser="lxml"):
202
203 filehandle = tempfile.NamedTemporaryFile()
204 filename = filehandle.name
205
206 data = rdoc(num_elements)
207 vars = dict(bs4=bs4, data=data, parser=parser)
208 cProfile.runctx('bs4.BeautifulSoup(data, parser)' , vars, vars, filename)
209
210 stats = pstats.Stats(filename)
211 # stats.strip_dirs()
212 stats.sort_stats("cumulative")
213 stats.print_stats('_html5lib|bs4', 50)
214
215if __name__ == '__main__':
216 diagnose(sys.stdin.read())