使用BeautifulSoup从单个博客存档页面提取多个帖子,无需脚本

克里斯蒂娜·P

我试图从一系列WordPress和Blogger博客存档页面中抓取作者,标题,日期和帖子内容。我已将页面保存下来,因此不会重复ping服务器。我已经完成了其他部分的工作,但是我似乎既无法从每个文件中获取所有帖子也无法从底部获取“添加到任何文件”或“可社交”或其他凌乱的脚本。我在这里

import urllib2
from bs4 import BeautifulSoup
import re

file_list = open ("hafiles.txt", "r")
posts_file = open ("haposts.txt","w")


for indurl in file_list:
    indurl = indurl.rstrip("\n")
    with open(indurl,"r") as ha_file:
     soup_ha = BeautifulSoup(ha_file)

    #works the second find gets rid of the sociable crap
    # this is the way it looks on the page <div class='post-body'>

    posts = soup_ha.find("div", class_="post-body").find_all("p")


    #tried a trick i saw on http://stackoverflow.com/questions/24458353/cleaning-text-string-after-getting-body-text-using-beautifulsoup
    #no joy
    #posts = soup_ha.find("div", class_="post-body")
    #text = [''.join(s.findAll(text=True))for s in posts.findAll('p')] 
    text = str(posts) + "\n" + "\n"
    posts_file.write (text)

print ("All done!")



file_list.close()
posts_file.close()

因此,如果我执行一次find_all并获得所有帖子(甚至不确定我是否实际上已全部获得),那么我将获得脚本。如果我仅使用find,至少可以通过两种方法获得没有脚本的漂亮帖子。我有一个文件列表,每个文件都有多个要提取的帖子。我在这里在stackoverflow和网络上进行了搜索。

eta:输入是一个非常混乱的网页,顶部是一堆脚本,页面上的所有css定义,然后

<div id='main-wrapper'>
<div class='main section' id='main'><div class='widget Blog' id='Blog1'>
<div class='blog-posts'>
<h2 class='date-header'>27 February, 2007</h2>
<div class='post uncustomized-post-template'>
<a name='edit'></a>
<h3 class='post-title'>
<a href='http:// edited for anon.html'>edit</a>
</h3>
<div class='post-header-line-1'></div>
<div class='post-body'>
<style>span.fullpost{display:none;}</style>
<p>edit this is post text - what i want</p>
<script type='text/javascript'>
          var permlink='edit';
          var title='edit';

          var spans = document.getElementsByTagName('span');
          var number = 0;
          for(i=0; i <spans.length; i++){
                var c = " " + spans[i].className + " ";
                if (c.indexOf("fullpost") != -1) {
                number++;
                }
                }

                if(number != memory){document.write('<p></p><a href=' + permlink + '>"'+ title + '" continues...</a>') }
           memory = number;
           </script>
<div style='clear: both;'></div>
</div>
<div class='post-footer'>
<p class='post-footer-line post-footer-line-1'>
<span class='post-author'>
Posted by
this is the author name, also want, have way to get
</span>
<span class='post-timestamp'>
at
<a class='timestamp-link' href='http://edit' title='permanent link'>2:53 pm</a>
</span>
<span class='post-comment-link'>
<a class='comment-link' href='edit' onclick=''>1 comments</a>
</span>
<span class='post-backlinks post-comment-link'>
<a class='comment-link' href='edit'>Links to this post</a>
</span>
<span class='post-icons'>
<span class='item-control blog-admin pid-edit'>
<a href='edit' title='Edit Post'>
<img alt='' class='icon-action' height='18' src='http://img2.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/>
</a>
</span>
</span>
</p>
<p class='post-footer-line post-footer-line-2'>
<span class='post-labels'>
Labels:
<a href='edit' rel='tag'>edi</a>
</span>
</p>
<p class='post-footer-line post-footer-line-3'></p>
</div>
</div>
<h2 class='date-header'>26 February, 2007</h2>
<div class='post uncustomized-post-template'>
<a name='5518681505930320089'></a>
<h3 class='post-title'>
<a href='edit'>edit</a>
</h3>
<div class='post-header-line-1'></div>
<div class='post-body'>
<style>span.fullpost{display:none;}</style>
<p>edit post text, what I want.</p>
<script type='text/javascript'>
          var permlink='http://edit';
          var title='edit';

          var spans = document.getElementsByTagName('span');
          var number = 0;
          for(i=0; i <spans.length; i++){
                var c = " " + spans[i].className + " ";
                if (c.indexOf("fullpost") != -1) {
                number++;
                }
                }

                if(number != memory){document.write('<p></p><a href=' + permlink + '>"'+ title + '" continues...</a>') }
           memory = number;
           </script>
<div style='clear: both;'></div>
</div>
<div class='post-footer'>
<p class='post-footer-line post-footer-line-1'>
<span class='post-author'>
Posted by
edit author name
</span>
<span class='post-timestamp'>
at
<a class='timestamp-link' href='edit' title='permanent link'>9:00 am</a>
</span>
<span class='post-comment-link'>
<a class='comment-link' href='edit' onclick=''>5
comments</a>
</span>
<span class='post-backlinks post-comment-link'>
<a class='comment-link' href='edit'>Links to this post</a>
</span>
<span class='post-icons'>
<span class='item-control blog-admin pid-edit'>
<a href='edit' title='Edit Post'>
<img alt='' class='icon-action' height='18' src='http://img2.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/>
</a>
</span>
</span>
</p>
<p class='post-footer-line post-footer-line-2'>
<span class='post-labels'>
Labels:
<a href='edit' rel='tag'>edit</a>,
<a href='edit' rel='tag'>edit</a>
</span>
</p>
<p class='post-footer-line post-footer-line-3'></p>
</div>
</div>
<h2 class='date-header'>22 February, 2007</h2>
<div class='post uncustomized-post-template'>
<a name='edit'></a>

所以我可能有20个左右的文件,每个文件中都有1到10个帖子(这有2个)...会很可爱的是csv或excel文件,就像日期作者标题postcontent

列,每行一行。我将使用一个仅包含帖子内容的文件,每个帖子之间有一些空间。我可以使用文本中的某些链接,粗体和列表以及其他功能,但是我不希望所有凌乱的脚本。谢谢

ec

这是一个包含多个帖子的单个页面的示例:

from bs4 import BeautifulSoup


soup = BeautifulSoup(open('test.html'))
posts = []
for post in soup.find_all('div', class_='post'):
    title = post.find('h3', class_='post-title').text.strip()
    author = post.find('span', class_='post-author').text.replace('Posted by', '').strip()
    content = post.find('div', class_='post-body').p.text.strip()
    date = post.find_previous_sibling('h2', class_='date-header').text.strip()

    posts.append({'title': title,
                  'author': author,
                  'content': content,
                  'date': date})
print posts

对于您发布的html,它打印:

[{'content': u'edit this is post text - what i want', 
  'date': u'27 February, 2007', 
  'author': u'this is the author name, also want, have way to get', 
  'title': u'edit'}, 
 {'content': u'edit post text, what I want.', 
  'date': u'26 February, 2007', 
  'author': u'edit author name', 
  'title': u'edit'}]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用BeautifulSoup从单个博客存档页面提取多个帖子,无需脚本

来自分类Dev

打破大量数据以显示在多个页面或列表中,无需脚本

来自分类Dev

无需脚本即可共享社交媒体

来自分类Dev

在帖子存档页面上使用分页链接

来自分类Dev

使用 PDFBox 从单个 PDF 页面中提取多个嵌入图像

来自分类Dev

Github页面上重定向的Octopress博客仅显示存档链接,而不显示最新帖子

来自分类Dev

无需脚本即可进行弹性搜索的部分更新

来自分类Dev

无需脚本即可进行弹性搜索的部分更新

来自分类Dev

使用Haskell从zip存档中提取单个文件

来自分类Dev

对特定的自定义帖子使用“自定义帖子存档页面”

来自分类Dev

python beautifulsoup抓取存档页面

来自分类Dev

使用Python BeautifulSoup从具有多个相同名称表的特定页面提取数据表

来自分类Dev

使用 BeautifulSoup 从抓取的页面中提取文本

来自分类Dev

WordPress如何检查当前页面是显示多个帖子还是显示单个帖子?

来自分类Dev

WordPress存档页面“无帖子”添加

来自分类Dev

在一个参数上运行两个命令(无需脚本)

来自分类Dev

使用插件在wordpress博客的每个页面和每个帖子中插入html的最佳方法是什么?

来自分类Dev

如何使用 php 将来自网站或博客的帖子嵌入 Facebook 页面

来自分类Dev

单个页面上的多个自定义帖子循环是否会影响页面速度

来自分类Dev

使用TIMBER在自定义帖子类型存档页面上显示SELECT字段

来自分类Dev

如何从cpio存档中提取单个文件?

来自分类Dev

使用单个连接编写多个帖子请求-PHP

来自分类Dev

使用BeautifulSoup在多个页面上编写循环

来自分类Dev

使用BeautifulSoup和Python刮取多个页面

来自分类Dev

Web使用BeautifulSoup抓取多个页面

来自分类Dev

使用 BeautifulSoup 在 python 中抓取多个页面

来自分类Dev

如何在单个WordPress帖子前添加博客目录?

来自分类Dev

当一个博客包含多个帖子时,如何模拟博客和帖子

来自分类Dev

如何使用Django博客撰写复杂的帖子

Related 相关文章

  1. 1

    使用BeautifulSoup从单个博客存档页面提取多个帖子,无需脚本

  2. 2

    打破大量数据以显示在多个页面或列表中,无需脚本

  3. 3

    无需脚本即可共享社交媒体

  4. 4

    在帖子存档页面上使用分页链接

  5. 5

    使用 PDFBox 从单个 PDF 页面中提取多个嵌入图像

  6. 6

    Github页面上重定向的Octopress博客仅显示存档链接,而不显示最新帖子

  7. 7

    无需脚本即可进行弹性搜索的部分更新

  8. 8

    无需脚本即可进行弹性搜索的部分更新

  9. 9

    使用Haskell从zip存档中提取单个文件

  10. 10

    对特定的自定义帖子使用“自定义帖子存档页面”

  11. 11

    python beautifulsoup抓取存档页面

  12. 12

    使用Python BeautifulSoup从具有多个相同名称表的特定页面提取数据表

  13. 13

    使用 BeautifulSoup 从抓取的页面中提取文本

  14. 14

    WordPress如何检查当前页面是显示多个帖子还是显示单个帖子?

  15. 15

    WordPress存档页面“无帖子”添加

  16. 16

    在一个参数上运行两个命令(无需脚本)

  17. 17

    使用插件在wordpress博客的每个页面和每个帖子中插入html的最佳方法是什么?

  18. 18

    如何使用 php 将来自网站或博客的帖子嵌入 Facebook 页面

  19. 19

    单个页面上的多个自定义帖子循环是否会影响页面速度

  20. 20

    使用TIMBER在自定义帖子类型存档页面上显示SELECT字段

  21. 21

    如何从cpio存档中提取单个文件?

  22. 22

    使用单个连接编写多个帖子请求-PHP

  23. 23

    使用BeautifulSoup在多个页面上编写循环

  24. 24

    使用BeautifulSoup和Python刮取多个页面

  25. 25

    Web使用BeautifulSoup抓取多个页面

  26. 26

    使用 BeautifulSoup 在 python 中抓取多个页面

  27. 27

    如何在单个WordPress帖子前添加博客目录?

  28. 28

    当一个博客包含多个帖子时,如何模拟博客和帖子

  29. 29

    如何使用Django博客撰写复杂的帖子

热门标签

归档