python beautiful soup爬虫学习


IMDBの映画のキーワードkeywordソースHTMLドキュメントに登り、ドキュメントのソースコードを参照してください。
# -*- coding: utf-8 -*-
import  urllib2
from bs4 import BeautifulSoup
import unicodedata
page=urllib2.urlopen("http://www.imdb.com/title/tt1619029/keywords?ref_=tt_stry_kw")
soup=BeautifulSoup(page,"lxml")
print soup.find_all(attrs={"class":"sodatext"})#       
print soup.select(' div[class="sodatext"]')#     
f=open('F:\\keyw.txt','w')#        
kwinfo=[]#  ,      

for keyw in soup.select(' div[class="sodatext"]'):
    kw=keyw.get_text()
    #print k
    kw.strip()
    line = unicodedata.normalize('NFKD', kw).encode('ascii', 'ignore')# Unicode     str  
    if(line.startswith("
"
)):# , line=line.replace("
"
,"")# print type(line) kwinfo.append(line)# print line print kwinfo# for item in range(len(kwinfo)): f.write(kwinfo[item]+"|")# , , | f.close()#
loosely based on real events widow period film 1960 s american polity jackie kennedy title spoken by character character name in title‘first lady’、‘american history’、‘kubriickian’、‘34 year old’、‘forename as title’、‘one word title’、‘female lead’、‘kennedy asasination’、‘death of husband’、‘year 1963’、‘lofidedededededededededededededededededededefilds’、‘Efifilds’、‘ファッションファッションファッションファッションファッションファッションファッションファッションファッションファッションファッションファッションファッションファッションバンド’、‘attttttbanks’、’、‘Ededel''''''''''''''''''''''''''''edy、‘ti’tle spoken by character、‘character name in title’’
書き込みドキュメント:first lady?american history kubrikin?34 year old?forenameas title?onewodtitle female lead?kendinasinination?deaaah of sbadbarereaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffrererereaaaaaaaaaaaam m m m m m m m m m m erican polity jackie kennedy(124; title spoken)by character name in title?