利用python爬取豆瓣电影Top250的相关信息,包括电影详情链接,图片链接,影片中文名,影片外国名,评分,评价数,概况,导演,主演,年份,地区,类别这12项内容,然后将爬取的信息写入Excel表中。基本上爬取结果还是挺好的。具体代码如下:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from bs4 import BeautifulSoup
import re
import urllib2
import xlwt
#得到页面全部内容
def askURL(url):
request = urllib2.Request(url)#发送请求
try:
response = urllib2.urlopen(request)#取得响应
html= response.read()#获取网页内容
#print html
except urllib2.URLError, e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason
return html
#获取相关内容
def getData(baseurl):
findLink=re.compile(r'<a href="(.*?)">')#找到影片详情链接