Fix for html5lib handling noscript oddly, noticed with fictionalley.org.

This commit is contained in:
Jim Miller
2016-01-30 12:19:19 -06:00
parent 784375d15e
commit 22e916bda9
2 changed files with 18 additions and 4 deletions
@@ -203,11 +203,14 @@ class FictionAlleyOrgSiteAdapter(BaseSiteAdapter):
# Yes, it's an evil kludge, but what can ya do? Using
# something other than div prevents soup from pairing
# our div with poor html inside the story text.
data = data.replace('<!-- headerend -->','<crazytagstringnobodywouldstumbleonaccidently id="storytext">').replace('<!-- footerstart -->','</crazytagstringnobodywouldstumbleonaccidently>')
crazy = "crazytagstringnobodywouldstumbleonaccidently"
data = data.replace('<!-- headerend -->','<'+crazy+' id="storytext">').replace('<!-- footerstart -->','</'+crazy+'>')
# problems with some stories confusing Soup. This is a nasty
# hack, but it works.
data = data[data.index("<crazytagstringnobodywouldstumbleonaccidently"):]
data = data[data.index('<'+crazy+''):]
# ditto with extra crap at the end.
data = data[:data.index('</'+crazy+'>')+len('</'+crazy+'>')]
soup = self.make_soup(data)
body = soup.findAll('body') ## some stories use a nested body and body
@@ -218,7 +221,7 @@ class FictionAlleyOrgSiteAdapter(BaseSiteAdapter):
text = body[1]
text.name='div' # force to be a div to avoid multiple body tags.
else:
text = soup.find('crazytagstringnobodywouldstumbleonaccidently', {'id' : 'storytext'})
text = soup.find(crazy, {'id' : 'storytext'})
text.name='div' # change to div tag.
if not data or not text:
+12 -1
View File
@@ -640,10 +640,21 @@ class BaseSiteAdapter(Configurable):
Convenience method for getting a bs4 soup. Older and
non-updated adapters call the included bs3 library themselves.
'''
## html5lib handles <noscript> oddly. See:
## https://bugs.launchpad.net/beautifulsoup/+bug/1277464
## This should 'hide' and restore <noscript> tags.
data = data.replace("noscript>","fff_hide_noscript>")
## soup and re-soup because BS4/html5lib is more forgiving of
## incorrectly nested tags that way.
soup = bs4.BeautifulSoup(data,'html5lib')
return bs4.BeautifulSoup(unicode(soup),'html5lib')
soup = bs4.BeautifulSoup(unicode(soup),'html5lib')
for ns in soup.find_all('fff_hide_noscript'):
ns.name = 'noscript'
return soup
def cachedfetch(realfetch,cache,url):
if url in cache: