良いスタートを切ったが、今はインデックスページを取得してBeautifulSoupパーサーにロードしているだけだ。リンクからのhrefを取得したので、基本的にこれらのリンクをすべて開き、それらのコンテンツをデータ構造にロードして、分析に使用できるようにする必要があります。
これは本質的に非常に単純なWebクローラーに相当します。他の人のコードを使用できる場合は、「pythonWebクローラー」をグーグルで検索することで適切なものが見つかる可能性があります。私はそれらのいくつかを見てきました、そしてそれらは十分に簡単ですが、このタスクにはやり過ぎかもしれません。ほとんどのWebクローラーは、再帰を使用して、特定のサイトのツリー全体をトラバースします。あなたの場合はもっと単純なもので十分なようです。
私がBeautifulSoupに慣れていないことを考えると、この基本的な構造によって、正しい方向に進むことができます。または、Webクロールがどのように行われるかを理解できます。
from BeautifulSoup import BeautifulSoup
import urllib2, re
emailContents = []
def analyze_emails():
# this function and any sub-routines would analyze the emails after they are loaded into a data structure, e.g. emailContents
def parse_email_page(link):
print "opening " + link
# open, soup, and parse the page.
#Looks like the email itself is in a "blockquote" tag so that may be the starting place.
#From there you'll need to create arrays and/or dictionaries of the emails' contents to do your analysis on, e.g. emailContents
def parse_list_page(link):
print "opening " + link
html = urllib2.urlopen(link).read()
soup = BeatifulSoup(html)
email_page_links = # add your own code here to filter the list page soup to get all the relevant links to actual email pages
for link in email_page_links:
parseEmailPage(link['href'])
def main():
html = urllib2.urlopen('http://www.419scam.org/emails/').read()
soup = BeautifulSoup(html)
links = soup.findAll(href=re.compile("20")) # I use '20' to filter links since all the relevant links seem to have 20XX year in them. Seemed to work
for link in links:
parse_list_page(link['href'])
analyze_emails()
if __name__ == "__main__":
main()