मैं सुंदरसुउप से सुंदरसूप 4 तक कुछ पार्सर माइग्रेट कर रहा हूं और मैंने सोचा कि यह देखना अच्छा होगा कि यह देखने के लिए कितना तेज़ होगा कि एलएक्सएमएल बहुत तेज़ है और यह पार्सर है जिसे मैं बीएस 4 के साथ उपयोग कर रहा हूंएलटीएमएल बनाम सुंदरसूपू के साथ सुंदरसूप 4
BS3 के लिए::
43208 function calls (42654 primitive calls) in 0.103 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:2(<module>)
18 0.000 0.000 0.000 0.000 <string>:8(__new__)
1 0.000 0.000 0.072 0.072 <string>:9(parser)
32 0.000 0.000 0.000 0.000 BeautifulSoup.py:1012(__init__)
1 0.000 0.000 0.000 0.000 BeautifulSoup.py:1018(buildTagMap)
...
BS4 के लिए का उपयोग कर lxml:
164440 function calls (163947 primitive calls) in 0.244 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.040 0.040 0.069 0.069 <string>:2(<module>)
18 0.000 0.000 0.000 0.000 <string>:8(__new__)
1 0.000 0.000 0.158 0.158 <string>:9(parser)
1 0.000 0.000 0.008 0.008 HTMLParser.py:1(<module>)
1 0.000 0.000 0.000 0.000 HTMLParser.py:54(HTMLParseError)
...
क्यों BS4
4 टिम बुला रहा है प्रोफ़ाइल परिणाम हैं अधिक कार्यों में? lxml
का उपयोग करने के लिए इसे सेट करने पर यह HTMLParser
का उपयोग क्यों कर रहा है?
सबसे महत्त्वपूर्ण चीजें मैं BS4 को BS3 से बदल इस प्रकार थे:
BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES) --->
BeautifulSoup(html, 'lxml')
[x.getText('**SEP**') for x in i.findChildren('font')[:2]] --->
[x.getText('**SEP**', strip=True) for x in i.findChildren('font')[:2]]
बाकी सब सिर्फ कुछ नाम परिवर्तन है (findParent तरह -> find_parent)
संपादित करें:
मेरी पर्यावरण:
python 2.7.3
beautifulsoup4==4.1.0
lxml==2.3.4
संपादित करें 2:
यहाँ इसे आज़माने के लिए एक छोटा सा कोड नमूना है:
from cProfile import Profile
from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup as BS4
import urllib2
def parse(html):
soup = BS4(html, 'lxml')
hl = soup.find_all('span', {'class': 'mw-headline'})
return [x.get_text(strip=True) for x in hl]
def parse3(html):
soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
hl = soup.findAll('span', {'class': 'mw-headline'})
return [x.getText() for x in hl]
if __name__ == "__main__":
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
html = ''.join(opener.open('http://en.wikipedia.org/wiki/Price').readlines())
profiler = Profile()
print profiler.runcall(parse, html)
profiler.print_stats()
profiler2 = Profile()
print profiler2.runcall(parse3, html)
profiler2.print_stats()
यदि आप हमें इस समस्या को प्रदर्शित करने के साथ काम करने के लिए नमूना यूआरएल नहीं देते हैं तो हम आपके परिणामों को पुन: पेश नहीं कर सकते हैं। (साथ ही, क्या आपने यह निर्धारित किया है कि lxml.html इस समस्या को प्रदर्शित करता है, या केवल बीएस 4?) –
केवल बीएस 4, ने अकेले एलएक्सएमएल के साथ यह कोशिश नहीं की है। मुझे एक आसान उदाहरण वास्तविक त्वरित बनाने दें ताकि आप इसे – Hassek
ठीक कर सकें ठीक है, बस थोड़ा उदाहरण जोड़ा गया है, इसलिए हर कोई – Hassek