Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Some ugly code to scrape blog posts and count how many, count total words, and count references. Attributes will differ depending on theme used and platform (made using wordpress).
from BeautifulSoup import BeautifulSoup
import mechanize
import time
import re
pagenum = 1
url = ""
browser = mechanize.Browser()
page =
postcount = 0
totalwordcount = 0
totalrefcount = 0
stop = "false"
while "false" in stop:
soup = BeautifulSoup(page)
link = soup.find("span", {"class":"next"})
# attributes probably need to be changed depending on the wordpress theme used
if "Older" in str(link):
pagenum += 1
print pagenum
stop = "false"
stop = "true"
for table in soup.findAll('h1', {'class':'title'}):
links = table.findAll('a')
if "href" in str(links):
#print links
separatelinks ="(?P<url>http?://[^\>\"\s]+)", str(links)).group("url")
print separatelinks
site =
soup = BeautifulSoup(site)
content = soup.find("div", {"class":"content entry-content"})
fp = open('C:/filename.html', 'w')
f = open('C:/filename.html', 'r')
readit =
onlytext = ''.join(BeautifulSoup(readit).findAll(text=True))
#print onlytext
fp2 = open('C:/filename.html', 'w')
words = re.findall('\w+', open('C:/filename.html').read().lower())
wordcount = len(words)
print wordcount
totalwordcount += wordcount
print totalwordcount
refcount = 0
for Z3988 in soup.findAll('span', {'class':'Z3988'}):
refcount += 1
print refcount
totalrefcount += refcount
print totalrefcount
postcount += 1
print postcount
#time.sleep(1) If you don't want to hit the server too fast
print "not a link"
url = ""+str(pagenum)
page =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment