python - 通过python加载网站内容

标签 python load

如何通过python从网站加载特定内容?例如,我想加载博客的一些帖子并将它们显示在我自己的网站上。我该怎么做?

最佳答案

一个答案:

import urllib2
from BeautifulSoup import BeautifulSoup

def fetchtags(req, name, attrs, num):
        try:
            website = urllib2.urlopen(req)
        except urllib2.HTTPError, e:
            print 'A problem occured. Please try again.'
            return
        soup = BeautifulSoup(website,
                             convertEntities=BeautifulSoup.HTML_ENTITIES)
        tags = soup.findAll(name=name,
                            attrs=attrs,
                            limit=num)
        return tags

然后你可以像这样使用它:

fetchtags('http://www.website.com', 'div', {'class':'c'}, 10)

从指定的 url 获取 10 个 c 类的 div...

有关返回对象的更多详细信息,请参阅 Beautiful Soup。

关于python - 通过python加载网站内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5434520/

相关文章:

c++ - 在 .exe 应用程序中加载 C++ DLL

javascript - 如何使用 JavaScript 在网站中滚动加载内容?

python - 当列表元素是需要解析的文本页时,如何将列表的每个元素存储在数据框中?

python - 如何使用 python-magic 通过 Internet 获取文件的文件类型?

python - Flask 服务器上出现 "Connection was reset"错误

javascript - jQuery 不能在 Chrome 中 $(...).load() 标题

javascript - 替换从外部for循环加载的html字符串中的字符

python - 奇怪的 undefined variable python3

python - Django:持久的数据库支持设置

python - 将 Pandas DatetimeIndex 转换为数字格式