爬虫小练习,面向对象,和函数式编程,爬取豆瓣电影

class Grasp:
def __init__(self):
pass
def getName(self):
for i in range(0,10):
self.url = f"https://movie.douban.com/top250?start={25*i}&filter="
self.html = urlopen(self.url).read().decode()
self.htmlobj = et.HTML(self.html)
self.res = self.htmlobj.xpath("//div[@class ='hd']/a/span[@class='title'][1]/text()") # 电影名

def getDir(self):
for i in range(0,10):
self.url = f"https://movie.douban.com/top250?start={25*i}&filter="
self.html = urlopen(self.url).read().decode()
self.htmlobj = et.HTML(self.html)
self.dicr = self.htmlobj.xpath("//div[@class ='bd']/p[1]/text()") # 导演

def getScore(self):
for i in range(0,10):
self.url = f"https://movie.douban.com/top250?start={25*i}&filter="
self.html = urlopen(self.url).read().decode()
self.htmlobj = et.HTML(self.html)
self.score = self.htmlobj.xpath("//div[@class ='star']/span[@class='rating_num']/text()") # 评分

def getCri(self):
for i in range(0,10):
self.url = f"https://movie.douban.com/top250?start={25*i}&filter="
self.html = urlopen(self.url).read().decode()
self.htmlobj = et.HTML(self.html)
self.cri = self.htmlobj.xpath("//p[@class ='quote']/span//text()") # 介绍

def run(self):
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('豆瓣电影')
for i in range(0,10):
self.url = f"https://movie.douban.com/top250?start={25*i}&filter="
self.html = urlopen(self.url).read().decode()
self.htmlobj = et.HTML(self.html)
self.res = self.htmlobj.xpath("//div[@class ='hd']/a/span[@class='title'][1]/text()") # 电影名
self.dicr = self.htmlobj.xpath("//div[@class ='bd']/p[1]/text()") # 导演
self.cri = self.htmlobj.xpath("//p[@class ='quote']/span//text()") # 介绍
self.score = self.htmlobj.xpath("//div[@class ='star']/span[@class='rating_num']/text()") # 评分
print((''.join(self.dicr)).strip().replace(' ', '').replace('/', '').split(',')[0:1])
for j in range(0, len(self.res)):
d = ((''.join(self.dicr[j * 2])).strip().replace(' ', '').replace('/', '')).find('主')
ws.write(i, j * 4, (''.join(self.res[j])).strip().replace(' ', '').replace('/', ''))
ws.write(i, 4 * j + 1, ((''.join(self.dicr[j * 2])).strip().replace(' ', '').replace('/', ''))[0:d])
try:
ws.write(i, 4 * j + 2, (''.join(self.cri[j])).strip())
except:
ws.write(i, 4 * j + 2,'没有介绍')
ws.write(i, 4 * j + 3, (''.join(self.score[j])).strip().replace(' ', '').replace('/', ''))
wb.save('./豆瓣电影/movie.xls')

r = Grasp()
r.run()


def reader():
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('豆瓣电影')
for i in range(0, 10):
url = f"https://movie.douban.com/top250?start={25*i}&filter="
html = urlopen(url).read().decode()
htmlobj = et.HTML(html)
res = htmlobj.xpath("//div[@class ='hd']/a/span[@class='title'][1]/text()") #电影名
dicr = htmlobj.xpath("//div[@class ='bd']/p[1]/text()") #导演
cri = htmlobj.xpath("//p[@class ='quote']/span//text()") #介绍
score = htmlobj.xpath("//div[@class ='star']/span[@class='rating_num']/text()") #评分
for j in range(0, len(res)):
d = ((''.join(dicr[j*2])).strip().replace(' ', '').replace('/', '')).find('主')
ws.write(i, j*4, (''.join(res[j])).strip().replace(' ','').replace('/',''))
ws.write(i, 4*j+1, ((''.join(dicr[j*2])).strip().replace(' ', '').replace('/', ''))[0:d])
try:
ws.write(i, 4*j+2, (''.join(cri[j])).strip())
except:
ws.write(i, 4 * j + 2,'没有介绍')
ws.write(i, 4*j+3, (''.join(score[j])).strip().replace(' ', '').replace('/', ''))
wb.save('./豆瓣电影/movie.xls')

reader()

将爬取的数据存储为表格
''.join()这些是为了将数据转换为单纯的字符串,除去特殊的字符和空格,便于数据的查看
此外,需要注意的是xpath获取的是一个列表,可以用列表的方法进行操作,不需要进行多余的转化

 

转载于:https://www.cnblogs.com/superSmall/p/11502872.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬取豆瓣电影的评论,你可以使用Python编写一个爬虫程序。首先,你需要准备工作,包括导入相关库和设置爬取的网页地址。 你可以参考中提到的Python实现的豆瓣电影信息爬取功能来进行编写。根据这个例子,你可以使用`requests`库发送HTTP请求获取网页内容,使用`BeautifulSoup`库解析网页内容。你可以将评论的网页链接作为参数传递给爬虫程序。 为了简单起见,你可以先只爬取第一页的评论内容,然后根据需要逐步爬取更多的评论。你可以参考中提供的链接来修改爬取地址的start值来获取更多的评论内容。 请注意,豆瓣网站对爬虫有一定的限制,你需要设置合理的请求头信息来模拟浏览器访问。 在爬取评论内容时,你可以使用CSS选择器或XPath表达式来定位评论所在的HTML元素,并提取出评论内容。 最后,你可以将爬取到的评论保存到文件或数据库中,或者进行进一步的处理和分析。 总结起来,爬取豆瓣电影评论的步骤如下: 1. 准备工作,导入相关库,设置爬取网页地址; 2. 发送HTTP请求获取网页内容; 3. 使用BeautifulSoup解析网页内容,定位评论所在的HTML元素,并提取评论内容; 4. 根据需要修改爬取地址的start值获取更多的评论; 5. 将评论保存到文件或数据库中,或进行进一步处理和分析。 希望这个回答对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值