鱼C论坛

 找回密码
 立即注册
查看: 716|回复: 1

爬取豆瓣排行榜

[复制链接]
发表于 2018-9-4 23:13:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import lxml.html
  3. import csv

  4. #获取目标网页
  5. doubanUrl = "https://movie.douban.com/top250?start={}&filter="

  6. # 解析网页
  7. # 获取网页数据
  8. def getSource(url):
  9.     response = requests.get(url)
  10.     response.encoding = "utf-8"
  11.     return response.content

  12. # 定义函数获取每一条电影的信息
  13. def getEveryItem(source):
  14.     selector = lxml.html.document_fromstring(source)

  15.     # 获取每一条电影的信息,并放在一个集合里面
  16.     movieItemList = selector.xpath('//div[class="info"]') # 获取所有属性info的div标签的信息

  17.     # 定义一个列表 目的:展示信息
  18.     movieList = []

  19.     # 用for循环把电影信息展开
  20.     for eachMovie in movieItemList:
  21.         # 保存电影 名字 地址 评分----
  22.         # 把字典里的信息用列表展示[{ movieDict1},{movieDict2}]
  23.         movieDict = {}
  24.         title = eachMovie.xpath('div[@class="hd"]/a/span[@class="title"]/text()') # 标题
  25.         otherTitle = eachMovie.xpath('div[@class="hd"]/a/span[@class="other"]/text()')  # 副标题
  26.         link = eachMovie.xpath('div[@class="hd"]/a/@href')[0] # url
  27.         star = eachMovie.xpath('div[@class="bd"]/div[@class="star"]/span[@class=""]/text()')[0]
  28.         quote = eachMovie.xpath('div[@class="bd"]/p[@class="quote"]/span/text()')[0]
  29.         print(title)
  30.         print("信息展开成功")


  31.         #保存到字典中
  32.         movieDict['title'] = ''.join(title + otherTitle)
  33.         movieDict['url'] = link
  34.         movieDict['star'] = star
  35.         movieDict['quote'] = quote
  36.         print(movieDict)
  37.         movieList.append(movieDict)
  38.         print("保存字典成功")

  39.     return movieList

  40. #下载目标网页数据

  41. def writeData(movieList):
  42.     with open('./MovieDouban.csv', 'w', encoding = 'utf-8') as f:
  43.         writer = csv.DictWriter(f,fieldnames=['title','star','quote','url'])
  44.         writer.writeheader() # 写表头
  45.         # 传入一个列表,每个元素代表一行
  46.         for each in movieList:
  47.             writer.writerow(each)


  48. if __name__ == '__main__':
  49.     movieList = []
  50.     # 分析 250部电影每页25个
  51.     for i in range(10):
  52.         # 处理定义好的url
  53.         pageLink = doubanUrl.format(i*25)

  54.         #print(pageLink)

  55.         # 调用获取网页信息函数
  56.         source = getSource(pageLink)

  57.         # 获取网页数据
  58.         movieList += getEveryItem(source)

  59.     print(movieList[:10])
  60.     writeData(movieList)
  61.     print('成功')

复制代码

大体步骤及其相关函数:
        1.获取目标网页:doubanUrl = "https://movie.douban.com/top250?start={}&filter="
        2.解析网页:getSource(url):
                2.1得到网页电影信息
                2.2展开网页电影信息
                2.3将展开的信息用字典保存
        3.下载网页数据 :writeData(movieList)

测试结果为,未爬取到数据
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-5 00:00:00 From FishC Mobile | 显示全部楼层
看看这个,
https://fishc.com.cn/forum.php?mod=viewthread&tid=120798
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 09:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表