鱼C论坛

 找回密码
 立即注册
查看: 2077|回复: 5

[已解决]请教一下 ,这段爬虫为啥爬不出东西???

[复制链接]
发表于 2019-12-25 20:28:06 | 显示全部楼层 |阅读模式

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

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

x
import urllib.request
import urllib.error
import re
import sys

class MovieComment():
    def __init__(self):

        self.start=0
        self.param='&filter=&type='
        self.headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
        self.commentist=[]
        self.filepath='E:/Projects/Python-Spiders/Python网络爬虫-罗攀/comment.txt'

    def getPage(self):
        try:
            for i in range(0,100,20):
                url = 'https://movie.douban.com/subject/25823277/comments?start=60&limit={i}&sort=new_score&status=P'
                request = urllib.request.Request(url,headers=self.headers)
                reponse = urllib.request.urlopen(request)
                page = reponse.read().decode('utf-8')
                print('正在抓取第' + str(i) + '页数据')
                return page
        except urllib.error.URLError as e:
            if hasattr(e,"reason"):
                print('抓取失败',e.reason)
    def getMovie(self):
        pattern=re.compile(u'<div.*?class="avatar">.*?'
                             + u'<a.*?title="(.*?)".*?href=".*?">.*?</a>.*?'
                             + u'<p.*?class="">(.*?)</p>',re.S)
        page=self.getPage()
        comments=re.findall(pattern,page)
        for comment in comments:
            self.commentist.append([comment[0],comment[1].strip()])
            with open('comment.txt','wb') as file:
                file.write(self.commentist.append([comment[0],comment[1].strip()]))

                file.close()
    def main(self):
        print('正在从《三生三世十里桃花》电影短评中抓取数据...')
        self.getMovie()
        print('抓取完毕...')

DouBanSpider = MovieComment()
DouBanSpider.main()
最佳答案
2019-12-25 20:32:07
代码很多问题,帮楼主修改了。

  1. import urllib.request
  2. import urllib.error
  3. import re


  4. class MovieComment:
  5.     def __init__(self):

  6.         self.start = 0
  7.         self.param = '&filter=&type='
  8.         self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, '
  9.                                       'like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
  10.         self.commentist = []
  11.         self.filepath = 'E:/Projects/Python-Spiders/Python网络爬虫-罗攀/comment.txt'

  12.     def getPage(self):
  13.         try:
  14.             for i in range(0, 100, 20):
  15.                 url = 'https://movie.douban.com/subject/25823277/comments?start=60&limit={i}&sort=new_score&status=P'
  16.                 request = urllib.request.Request(url, headers=self.headers)
  17.                 reponse = urllib.request.urlopen(request)
  18.                 page = reponse.read().decode('utf-8')
  19.                 print('正在抓取第' + str(i) + '页数据')
  20.                 return page
  21.         except urllib.error.URLError as e:
  22.             if hasattr(e, "reason"):
  23.                 print('抓取失败', e.reason)

  24.     def getMovie(self):
  25.         pattern = re.compile(u'<div.*?class="avatar">.*?'
  26.                              + u'<a.*?title="(.*?)".*?href=".*?">.*?</a>.*?'
  27.                              + u'<p.*?class="">(.*?)</p>', re.S)
  28.         page = self.getPage()
  29.         comments = re.findall(pattern, page)
  30.         for comment in comments:
  31.             self.commentist.append([comment[0], comment[1].strip()])
  32.             with open('comment.txt', 'wb') as file:
  33.                 print(comment[0])
  34.                 self.commentist.append([comment[0], comment[1].strip()])
  35.                 file.write(" ".join([comment[0], comment[1].strip()]).encode())

  36.     def main(self):
  37.         print('正在从《三生三世十里桃花》电影短评中抓取数据...')
  38.         self.getMovie()
  39.         print('抓取完毕...')


  40. DouBanSpider = MovieComment()
  41. DouBanSpider.main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-25 20:30:54 | 显示全部楼层
  1. url = 'https://movie.douban.com/subject/25823277/comments?start=60&limit={i}&sort=new_score&status=P'
复制代码

这是什么语法,其他的暂时没看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-25 20:32:07 | 显示全部楼层    本楼为最佳答案   
代码很多问题,帮楼主修改了。

  1. import urllib.request
  2. import urllib.error
  3. import re


  4. class MovieComment:
  5.     def __init__(self):

  6.         self.start = 0
  7.         self.param = '&filter=&type='
  8.         self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, '
  9.                                       'like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
  10.         self.commentist = []
  11.         self.filepath = 'E:/Projects/Python-Spiders/Python网络爬虫-罗攀/comment.txt'

  12.     def getPage(self):
  13.         try:
  14.             for i in range(0, 100, 20):
  15.                 url = 'https://movie.douban.com/subject/25823277/comments?start=60&limit={i}&sort=new_score&status=P'
  16.                 request = urllib.request.Request(url, headers=self.headers)
  17.                 reponse = urllib.request.urlopen(request)
  18.                 page = reponse.read().decode('utf-8')
  19.                 print('正在抓取第' + str(i) + '页数据')
  20.                 return page
  21.         except urllib.error.URLError as e:
  22.             if hasattr(e, "reason"):
  23.                 print('抓取失败', e.reason)

  24.     def getMovie(self):
  25.         pattern = re.compile(u'<div.*?class="avatar">.*?'
  26.                              + u'<a.*?title="(.*?)".*?href=".*?">.*?</a>.*?'
  27.                              + u'<p.*?class="">(.*?)</p>', re.S)
  28.         page = self.getPage()
  29.         comments = re.findall(pattern, page)
  30.         for comment in comments:
  31.             self.commentist.append([comment[0], comment[1].strip()])
  32.             with open('comment.txt', 'wb') as file:
  33.                 print(comment[0])
  34.                 self.commentist.append([comment[0], comment[1].strip()])
  35.                 file.write(" ".join([comment[0], comment[1].strip()]).encode())

  36.     def main(self):
  37.         print('正在从《三生三世十里桃花》电影短评中抓取数据...')
  38.         self.getMovie()
  39.         print('抓取完毕...')


  40. DouBanSpider = MovieComment()
  41. DouBanSpider.main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 20:37:41 | 显示全部楼层
zltzlt 发表于 2019-12-25 20:32
代码很多问题,帮楼主修改了。

首先感谢您的回复  
您的代码我试了,可以跑 但是只能抓一页??我中间写了一个循环  为啥没有起到作用??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-25 20:42:28 | 显示全部楼层
用这段代码:

  1. import urllib.request
  2. import urllib.error
  3. import re


  4. class MovieComment:
  5.     def __init__(self):

  6.         self.start = 0
  7.         self.param = '&filter=&type='
  8.         self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, '
  9.                                       'like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
  10.         self.commentist = []
  11.         self.filepath = 'E:/Projects/Python-Spiders/Python网络爬虫-罗攀/comment.txt'

  12.     def getPage(self):
  13.         try:
  14.             for i in range(0, 100, 20):
  15.                 url = f'https://movie.douban.com/subject/25823277/comments?start=60&limit=' + \
  16.                       f'{i}&sort=new_score&status=P'
  17.                 request = urllib.request.Request(url, headers=self.headers)
  18.                 reponse = urllib.request.urlopen(request)
  19.                 page = reponse.read().decode('utf-8')
  20.                 print('正在抓取第' + str(i) + '页数据')
  21.                 yield page
  22.         except urllib.error.URLError as e:
  23.             if hasattr(e, "reason"):
  24.                 print('抓取失败', e.reason)

  25.     def getMovie(self):
  26.         pattern = re.compile(u'<div.*?class="avatar">.*?'
  27.                              + u'<a.*?title="(.*?)".*?href=".*?">.*?</a>.*?'
  28.                              + u'<p.*?class="">(.*?)</p>', re.S)
  29.         page = "".join(list(self.getPage()))
  30.         comments = re.findall(pattern, page)
  31.         for comment in comments:
  32.             self.commentist.append([comment[0], comment[1].strip()])
  33.             with open('comment.txt', 'wb') as file:
  34.                 print(comment[0])
  35.                 self.commentist.append([comment[0], comment[1].strip()])
  36.                 file.write(" ".join([comment[0], comment[1].strip()]).encode())

  37.     def main(self):
  38.         print('正在从《三生三世十里桃花》电影短评中抓取数据...')
  39.         self.getMovie()
  40.         print('抓取完毕...')


  41. DouBanSpider = MovieComment()
  42. DouBanSpider.main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 21:15:59 | 显示全部楼层

多谢回复~~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-1 21:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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