按youtube上面的教程自己一个一个代码敲的。
主要功能:
检查目标网页的on-page SEO效果,比如title,keywords,h1,h2这些。
扩展用途:
可以用于分析搜索引擎的搜索结果。
成果展示:
from urllib.request import urlopen,Request from bs4 import BeautifulSoup import re url = input("Which page would you like to check? Enter Full URL:") keyword = input("what is your seo keyword?") keyword = keyword.casefold() try: req = Request(url, headers = {'User-Agent':'Mozilla/6.0'}) html = urlopen(req) except HTTPError as e: print(e) data = BeautifulSoup(html,"html.parser") def seo_title_found(keyword,data): if data.title: if keyword in data.title.text.casefold(): status = "Found" else: status = "Not Found" else: status = "No title found" return status def seo_title_stop_words(data): words = 0 list_words = [] if data.title: with open('stopwords.txt','r') as f: for line in f: if re.search(r'\b' + line.rstrip('\n')+r'\b',data.title.text.casefold()): words += 1 list_words.append(line.rstrip('\n')) if words > 0: stop_words = "We found {} stop words in your title. You should consider removing them.{}".format(words,list_words) else: stop_words = "We found no stop words in the title. Good work!" else: stop_words = "We could not find a title" return stop_words def seo_title_length(data): if data.title: if len(data.title.text) < 60: length = "Your length is under the maximum suggested length of 60 characters. Your title is {}".format(len(data.title.text)) else: length = "Your length is over the maximum suggest length of 60 character. Your title is {}".format(len(data.title.text)) else: length = "No title was found" return length def se_url(url): if url: if keyword in url: slug = "Your keyword was found in your slug" else: slug = "Your keywords was not found in your slug" else: slug = "No url was retured" return slug def seo_url_length(url): if url: if len(url) < 100: url_length = "Your URL is less than the 100 character maximum suggested length.Good work" else: url_length = "Your URL is over 100 characters. Your URL currently is {}.You should change this.".format(len(url)) else: url_length = "URL was not found" return url_length def seo_h1(keyword, data): total_h1 = 0 total_keyword_h1 = 0 if data.h1: all_tags = data.find_all('h1') for tag in all_tags: total_h1 += 1 tag = str(tag.string) if keyword in tag.casefold(): total_keyword_h1 += 1 h1_tag = "Found keyword in h1 tag.You have a total of {} H1 Tags.and your kwyword was found in {} of them".format(total_h1,total_keyword_h1) else: h1_tag = "Didn't find a kwyords in h1 tag." else: h1_tag = "No H1 Tags Found" return h1_tag def seo_h2(keyword,daa): if data.h2: all_tags = data.find_all('h2') for tag in all_tags: tag = str(tag.string) if keyword in tag.casefold(): h2_tag = "Found your keyword in at least one h2 tag" else: h2_tag = "We didn't find your keyword in a singel h2 tag.You should add {} to h2 tag".format(keyword) else: h2_tag = "No h2 tags found.You should have at least one containing your keyword." return h2_tag print(seo_title_found(keyword,data)) print(seo_title_stop_words(data)) print(seo_title_length(data)) print(se_url(url)) print(seo_url_length(url)) print(seo_h1(keyword,data)) print(seo_h2(keyword,data))
https://www.youtube.com/watch?v=AJAf1-dy5RA