Example #1
0
    def ParsePageContent(self, topic, url, urls, count):
        # 请求主题页面链接并获取其内容
        result = self.GetResponseContent(url)
        # 如果请求成功,并且页面内容不为空
        if result.status_code == 200 and result.content:
            # 将页面内容转换成BeatifulSoup对象
            soup = BeautifulSoup(result.content, 'lxml')
            # 找出当前页面文章列表中所有文章条目
            items = soup.find_all(name='span', class_='tw3_01_2_t')

            # 循环处理每个文章条目
            for item in items:
                title = item.a.string  # 获取文章标题
                link = item.a.get('href')  # 获取文章链接
                link = BaseFeedBook.urljoin(url, link)  # 合成文章链接
                count += 1  # 统计当前已处理的文章条目
                # 如果处理的文章条目超过了设定数量则中止抽取
                if count > self.max_articles_per_feed:
                    break
                # 如果文章发布日期超出了设定范围则忽略不处理
                if self.OutTimeRange(item):
                    continue
                # 将符合设定文章数量和时间范围的文章信息作为元组加入列表
                urls.append((topic, title, link, None))

            # 如果主题页面有下一页,且已处理的文章条目未超过设定数量,则继续抓取下一页
            next = soup.find(name='a', string='Next')
            if next and count < self.max_articles_per_feed:
                url = BaseFeedBook.urljoin(url, next.get('href'))
                self.ParsePageContent(topic, url, urls, count)
        # 如果请求失败则打印在日志输出中
        else:
            self.log.warn('Fetch article failed(%s):%s' % \
                (URLOpener.CodeMap(result.status_code), url))
Example #2
0
 def fetcharticle(self, url, opener, decoder):
     m = re.search(r'/(\d{8})/(\d{8,})', url)
     if m:
         pubdate=m.group(1)
         artnum=m.group(2)
         url = 'http://news.donga.com/View?gid=' + artnum +'&date=' + pubdate
     return BaseFeedBook.fetcharticle(self,url,opener,decoder)
Example #3
0
 def ParseFeedUrls(self):
     urls = [] # 定义一个空的列表用来存放文章元组
     # 循环处理fees中两个主题页面
     for feed in self.feeds:
         # 分别获取元组中主题的名称和链接
         topic, url = feed[0], feed[1]
         # 请求主题链接并获取相应内容
         opener = URLOpener(self.host, timeout=self.timeout)
         result = opener.open(url)
         # 如果请求成功,并且页面内容不为空
         if result.status_code == 200 and result.content:
             # 将页面内容转换成BeatifulSoup对象
             soup = BeautifulSoup(result.content, 'lxml')
             # 找出当前页面文章列表中所有文章条目
             items=soup.find('div',class_='grid').find_all(name='div', class_='content')
             # 循环处理每个文章条目
             for item in items:
                 title = item.span.string # 获取文章标题
                 link = item.a.get('href') # 获取文章链接
                 link = BaseFeedBook.urljoin(url, link) # 合成文章链接
                 if self.OutTimeRange(item):
                     continue
                 urls.append((topic, title, link, None)) # 把文章元组加入列表
         # 如果请求失败通知到日志输出中
         else:
             self.log.warn('Fetch article failed(%s):%s' % \
                 (URLOpener.CodeMap(result.status_code), url))
     # 返回提取到的所有文章列表
     return urls
Example #4
0
    def ParsePageLinks(self, topic, url, urls, count, count2, ccc):
        # 请求主题页面或章节列表页面的链接和内容
        result = self.GetResponseContent(url)
        # 如果请求成功,并且页面内容不为空
        if result.status_code == 200 and result.content:
            # 将主题或列表页面内容转换成BeatifulSoup对象
            soup = BeautifulSoup(result.content, 'lxml')
            # 找出当前页面文章列表中所有文章条目,里面的标签参数需要手工修改确认
            items = soup.find_all(name='dd')
            #获取总章节数,以便抓取最新章节,追更---应该有个函数能使用,可惜我不知道啊
            for ttt in items:
                ccc += 1

            # 循环处理每个文章条目
            for item in items:
                title = item.a.string  # 获取文章标题
                link = item.a.get('href')  # 获取文章链接
                link = BaseFeedBook.urljoin(url, link)  # 合成文章链接
                count += 1  # 统计当前已处理的文章条目

                # 如果处理的文章条目超过了设定数量则中止抽取,改动下面的条件限制,选择抓取方式,都屏蔽掉,则抓全部
                count2 = count + self.max_articles_per_feed
                if count2 < ccc:  #一、从最后抓n章
                    continue

                #if count > self.max_articles_per_feed:                                      #二、从前面抓n章
                #    break

                # 将符合设定文章数量的文章信息作为元组加入列表
                urls.append((topic, title, link, None))

            # 如果主题页面有下一页,且已处理的文章条目未超过设定数量,则继续抓取下一页,递进调用自己
            #next = soup.find(name='a', string='Next')
            #if next and count < self.max_articles_per_feed:
            #url = BaseFeedBook.urljoin(url, next.get('href'))
            #self.ParsePageLinks(topic, url, urls, count)
        # 如果请求失败则打印在日志输出中
        else:
            self.log.warn('Fetch article failed(%s):%s' % \
                (URLOpener.CodeMap(result.status_code), url))
Example #5
0
 def ParseFeedUrls(self):
     urls = [] # 定义一个空的列表用来存放文章元组
     # 循环处理fees中两个主题页面
     for feed in self.feeds:
         # 分别获取元组中主题的名称和链接
         topic, url = feed[0], feed[1]
         # 请求主题链接并获取相应内容
         opener = URLOpener(self.host, timeout=self.timeout)
         result = opener.open(url)
         # 如果请求成功,并且页面内容不为空
         if result.status_code == 200 and result.content:
             # 将页面内容转换成BeatifulSoup对象
             soup = BeautifulSoup(result.content, 'html.parser')
             # self.log.warn('title : %s' % soup.title)
             # 找出当前页面文章列表中所有文章条目'
             items = soup.find_all(name='div', class_="content")
             self.log.warn('find : %d articles.' % len(items))
             # 循环处理每个文章条目
             count = 0
             for item in items:
                 title = item.a.string # 获取文章标题
                 link = item.a.get('href') # 获取文章链接
                 link = BaseFeedBook.urljoin("https://toutiao.io", link) # 合成文章链接
                 link = self.getRealUrl (link)
                 self.log.warn('Fetch article : %s' % link)
                 if string.find (link, 'zhihu.com') != -1:
                     link = self.url4forwarder(url)
                     self.log.warn('transport : %s' % link)                        
                 urls.append((topic, title, link, None)) # 把文章元组加入列表
                 count = count + 1
                 if count >= 30 :
                     break
         # 如果请求失败通知到日志输出中
         else:
             self.log.warn('Fetch article failed(%s):%s' % \
                 (URLOpener.CodeMap(result.status_code), url))
     # 返回提取到的所有文章列表
     return urls
Example #6
0
    def ParsePageContent(self, topic, url, urls, count):
        url2 = 'https://www.biqudd.com'
        # 请求主题页面链接并获取其内容
        result = self.GetResponseContent(url)
        # 如果请求成功,并且页面内容不为空
        #print result.content
        if result.status_code == 200 and result.content:
            # 将页面内容转换成BeatifulSoup对象
            soup = BeautifulSoup(result.content, 'lxml')
            # 找出当前页面文章列表中所有文章条目
            items = soup.find_all(name='dd')
            #print(items)
            # 循环处理每个文章条目
            for item in items:
                title = item.a.string  # 获取文章标题
                print title
                link = item.a.get('href')  # 获取文章链接
                #print(link)
                link = BaseFeedBook.urljoin(url2, link)  # 合成文章链接

                count += 1  # 统计当前已处理的文章条目
                # 如果处理的文章条目超过了设定数量则中止抽取
                if count > self.max_articles_per_feed:
                    break
                # 如果文章发布日期超出了设定范围则忽略不处理
                #if self.OutTimeRange(item):
                #continue
                # 将符合设定文章数量和时间范围的文章信息作为元组加入列表
                urls.append((topic, title, link, None))

            # 如果主题页面有下一页,且已处理的文章条目未超过设定数量,则继续抓取下一页
            #next = soup.find(name='a', string='Next')
            #if next and count < self.max_articles_per_feed:
            #url = BaseFeedBook.urljoin(url, next.get('href'))
            #self.ParsePageContent(topic, url, urls, count)
        # 如果请求失败则打印在日志输出中
        else:
            print("asdf")
Example #7
0
    def ParseFeedUrls(self):
        urls = [] # 定义一个空的列表用来存放文章元组
        # 循环处理fees中两个主题页面
        for feed in self.feeds:
            # 分别获取元组中主题的名称和链接
            topic, url = feed[0], feed[1]
            # 请求主题链接并获取相应内容
            opener = URLOpener(self.host, timeout=self.timeout)
            result = opener.open(url)
            # 如果请求成功,并且页面内容不为空
            if result.status_code == 200 and result.content:
                # 将页面内容转换成BeatifulSoup对象
                soup = BeautifulSoup(result.content, 'lxml')
                # 找出当前页面文章列表中所有文章条目
                items = soup.find_all('li', attrs={'style':'height: 215px;'})
                # 循环处理每个文章条目
                for item in items:
					orz = soup.find_all('h4', attrs={'class':'module-title'})
                    title = orz.a.string # 获取文章标题
                    link = orz.a.get('href') # 获取文章链接
                    link = BaseFeedBook.urljoin(url, link) # 合成文章链接
                    urls.append((topic, title, link, None)) # 把文章元组加入列表
					pubdate = article.find(name='span').string
Example #8
0
 def ParseFeedUrls(self):
     urls = []  # 定义一个空的列表用来存放文章元组
     # 循环处理fees中两个主题页面
     for feed in self.feeds:
         # 分别获取元组中主题的名称和链接
         topic, url = feed[0], feed[1]
         # 请求主题链接并获取相应内容
         opener = URLOpener(self.host, timeout=self.timeout)
         result = opener.open(url)
         # 如果请求成功,并且页面内容不为空
         if result.status_code == 200 and result.content:
             # 将页面内容转换成BeatifulSoup对象
             soup = BeautifulSoup(result.content, 'html.parser')
             # 找出当前页面文章列表中所有文章条目'
             item = soup.find(name='dd')
             count = 0
             while item:
                 # 只获取最新更新章节
                 if item.name != 'dd':
                     break
                 title = item.a.string  # 获取文章标题
                 link = item.a.get('href')  # 获取文章链接
                 link = BaseFeedBook.urljoin(
                     "https://www.72wx.com", link)  # 合成文章链接
                 urls.insert(0, (topic, title, link, None))  # 把文章元组加入列表
                 count = count + 1
                 if count >= 20:
                     break
                 item = item.next_sibling
                 while type(item) != element.Tag:
                     item = item.next_sibling
         # 如果请求失败通知到日志输出中
         else:
             self.log.warn('Fetch article failed(%s):%s' %
                           (URLOpener.CodeMap(result.status_code), url))
     # 返回提取到的所有文章列表
     return urls
Example #9
0
 def processtitle(self, title):
     title = BaseFeedBook.processtitle(self,title)
     if title.endswith(u'-华尔街日报'):
         return title.replace(u'-华尔街日报','')
     else:
         return title
Example #10
0
 def fetcharticle(self, url, opener, decoder):
     #每个URL都增加一个后缀full=y,如果有分页则自动获取全部分页
     url += '?full=y'
     return BaseFeedBook.fetcharticle(self,url,opener,decoder)
     
Example #11
0
 def fetcharticle(self, url, opener, decoder):
     return BaseFeedBook.fetcharticle(self,url,opener,decoder)
     
Example #12
0
 def processtitle(self, title):
     title = BaseFeedBook.processtitle(self, title)
     if title.endswith(u'-华尔街日报'):
         return title.replace(u'-华尔街日报', '')
     else:
         return title
Example #13
0
 def fetcharticle(self, url, opener, decoder):
     #链接网页获取一篇文章
     return BaseFeedBook.fetcharticle(self, self.url4forwarder(url), opener,
                                      decoder)
Example #14
0
 def fetcharticle(self, url, decoder):
     #每个URL都增加一个后缀full=y,如果有分页则自动获取全部分页
     url += '/print'
     return BaseFeedBook.fetcharticle(self,url,decoder)
     
Example #15
0
 def fetcharticle(self, url, opener, decoder):
     #链接网页获取一篇文章
     return BaseFeedBook.fetcharticle(self, self.url4forwarder(url), opener, decoder)