kapt-labs / django-check-seo

Django Check SEO will check the SEO aspects of your site for you, and will provide advice in case of problems. Compatible with Django & Django-CMS!
GNU General Public License v3.0
154 stars 12 forks source link

Django check seo search in wrong content #35

Closed corentinbettiol closed 4 years ago

corentinbettiol commented 4 years ago

Currently, django-check-seo is searching the content in the <div class="container"></div> tag.

It could be a problem if the main content of the pages of the crawled website is in another tag (like <main> or <div class="cms_main"> ...).

We should provide a setting where we could select a tag/class/list of tags/classes to search content in.

corentinbettiol commented 4 years ago

Possible solution:

This css selector will only search the elements with class container directly inside elements with id wrap:

image

So the #wrap > header > .container and footer > .container elements will not be selected.

The hard part is to have a consistent layout on each page to always select the main content.

corentinbettiol commented 4 years ago

Using a list of selectors and the fabulous extract() function of BeautifulSoup4, we can extract the selected html nodes from our main soup, and only keep what we want in the soup (the main content).

This method will certainly be chosen, because headers, navigation and footers tend to have the same class names & id for each page of the site (content class names may vary if we use different page templates in our website).

Ex:

excluded_selectors = ".nav, .darksky-small, .cover-section, .footer"

for element in soup.select(excluded_selectors):
    element.extract()
    # [...]

print(soup)
<!doctype html>
<html>
<head>
    <title>Home</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="description" content="None">
    <meta name="keywords" content="kw1,  kw2,  kw3">
</head>
<body>
    <div class="container">
        <section class="container">Container content 0</section>
        <div id="wrap">
            <section class="container">Container content 1</section>
            <section class="container">Container content 2</section>
        </div>
    </div>
</body>
</html>

Edit: Here's a screenshot of the previous code:

image