关于抓取评论网站的问题

时间:2016-11-17 08:02:12

标签: python web-scraping web-crawler

我目前正在用美丽的汤爬行评论网站。

评论页面包含来自不同学生的评论, 每个学生都会在几个方面评估学校。

因此,页面的结构通常如下所示:

  1. 学生A - 头衔A:

    • 方面1
    • 对方面1的评论
    • aspect2
    • commet on aspect 2
    • aspect3
    • commet on aspect 3
  2. 学生B - 头衔B:

    • 方面1
    • 对方面1的评论
    • aspect2
    • commet on aspect 2
    • aspect4
    • commet on aspect 4
  3. 有些学生只对特定方面发表评论。他们不评论的方面将不会在网站上显示。

    代码中的每个评论

         <!-- mod-reviewTop -->
         <div class="mod-reviewTop">
          <!-- mod-reviewTop-inner -->
          <div class="mod-reviewTop-inner">
           <dl>
            <dd>
             <div class="mod-reviewTitle" itemprop="summary">
                title 1 : It was ok.
             </div>
            </dd>
           </dl>
            <!-- /mod-reviewItem -->
         </div>
         <!-- /mod-reviewTop -->
         <!-- mod-reviewBottom -->
         <div class="mod-reviewBottom">
          <!-- mod-reviewList-list -->
          <div class="mod-reviewList-list js-review-detail" itemprop="description">
           <!-- js-mod-reviewList-list -->
           <div class="js-mod-reviewList-list">
            <ul>
             <li>
              <div class="mod-reviewTitle3">
               Total Evaluation
              </div>
              <div class="mod-reviewList-txt">
               We can freely choose the course we want, and thus a lot of different knowledge can be learned.
              </div>
             </li>
             <li>
              <div class="mod-reviewTitle3">
               Course
              </div>
              <div class="mod-reviewList-txt">
               the courses are good.
              </div>
             </li>
             <li>
              <div class="mod-reviewTitle3">
               Lab
              </div>
              <div class="mod-reviewList-txt">
               we don’t join lab in the first 2 year.
              </div>
             </li>
            </ul>
           </div>
           <!-- /js-mod-reviewList-list -->
          </div>
          <!-- /mod-reviewList-list -->
         </div>
         <!-- /mod-reviewBottom -->
    

    你可以看到,即使方面的标题不同,它们都以&#39; div class =&#34; mod-reviewTitle3&#34;开头。 &#39; ,评论全部以&#39; div class =&#34; mod-reviewList-txt&#34;&#39; 开头。 我的问题是如何编写好的代码将这些信息存储到数据集中:

    |标题| aspect1评论| aspect2评论
         一个很好的非常好的

    我已经尝试了下面的代码,但每个块中的方面注释都不能正常工作

    datatest = soup.find_all("div", {"class":"mod-reviewTop"})
    datatest1 = soup.find_all("div", {"class":"mod-reviewBottom"})
    
    for item in datatest:
        a = item.select('.mod-reviewTitle')       
        c = item.select('.mod-reviewTitle3')
        d = item.select('.mod-reviewList-txt')  
        g = item.select('.js-mod-reviewList-list')
        f= item.select('.mod-reviewItem')   
    
    for i in range(len(a)):
        f1= f[i].text[7]
        f2= f[i].text[17]
        f3= f[i].text[26]
        f4= f[i].text[37]
        f5= f[i].text[46]
        f6= f[i].text[55]
        f7= f[i].text[63]
    
        print a[i].text
        print f1, f2, f3, f4, f5, f6, f7
        for item in datatest1:
            for k in range(len(g)):
                print g[k].text
                print e[k].text
                print k
    

    我认为这是一个编程问题.. 我尝试过循环,但效果不好

    如果你能给我一些参考或者结构如何在逻辑上起作用,请给我一个评论..谢谢

1 个答案:

答案 0 :(得分:1)

提示:

  • 您应该将aspectscomments附加到相应的titles,这意味着您可以使用适当的数据结构将它们存储在一起。像这样(只有一种可能的方式)

    [ (title1,[ (aspect1, comment1), (aspect2, comment2), ... ]), (title2,[ (aspect1, comment1), (aspect2, comment2), ... ]), ... ]

  • 因此,在检索所需数据时,请使用嵌套的for循环组织操作。例如,一旦找到方面,尝试获取相应的注释并将它们存储在一起。逃避找到所有方面,然后是所有评论。

代码

这是一个演示。

blocks = soup.find_all("div", {"class":"mod-reviewTop"})
contents = soup.find_all("div", {"class":"mod-reviewBottom"})
data = []

for i,block in enumerate(blocks):
    aspects = []
    title = str(block.find('div',{'class':'mod-reviewTitle'}).text).strip()
    for aspect_block in contents[i].find_all('li'):
        aspect = str(aspect_block.find('div',{'class':'mod-reviewTitle3'}).text).strip()
        comment = str(aspect_block.find('div',{'class':'mod-reviewList-txt'}).text).strip()
        aspects.append((aspect,comment))
    data.append((title,aspects))

print data

with open("output.txt","w") as file:
    for title, aspects in data:
        file.write(title)
        for aspect in aspects:
            file.write('|'+aspect[0]+'\t'+aspect[1])
        file.write('\n')
相关问题