您的位置:首页 > 编程语言 > Python开发

python beautifulsoup 爬虫学习

2016-12-01 21:03 549 查看
爬取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("\n")):#如果以换行符开头,
line=line.replace("\n","")#去除所有换行符
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

1960s

american politics

jackie kennedy

title spoken by character

character name in title

[‘first lady’, ‘american history’, ‘kubrickian’, ‘34 year old’, ‘forename as title’, ‘one word title’, ‘female lead’, ‘kennedy assassination’, ‘death of husband’, ‘year 1963’, ‘loosely based on real events’, ‘widow’, ‘period film’, ‘1960s’, ‘american politics’, ‘jackie kennedy’, ‘title spoken by character’, ‘character name in title’]

写入文档:first lady|american history|kubrickian|34 year old|forename as title|one word title|female lead|kennedy assassination|death of husband|year 1963|loosely based on real events|widow|period film|1960s|american politics|jackie kennedy|title spoken by character|character name in title|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 爬虫