【问题标题】:How can I create a stylesheet external link using BeautifulSoup?如何使用 BeautifulSoup 创建样式表外部链接?
【发布时间】:2016-01-02 22:17:05
【问题描述】:

带有“rel”、“href”和“type”的链接,就像:

<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">

只是为了证明我已经尝试过,这是我失败的尝试:

def add_css(self, *links):
    if links:
        new_soup = BeautifulSoup("<link>")
        for link in links:
            new_soup.attrs["rel"] = "stylesheet"
            new_soup.attrs["href"] = link
            new_soup.attrs["type"] = "text/css"
        self.soup.head.insert(0, new_soup)
        self.update_document()

输出:

<html>
<head><html><head><link/></head></html>
<title></title>
</head>
<body></body>
</html>

如您所见,那里有一个空链接标签。顺便说一句,我试过这样:

webpage.add_css("css/bootstrap.min.css")

【问题讨论】:

    标签: python css beautifulsoup


    【解决方案1】:

    我们直接创建如下:

    >>> new_soup = BeautifulSoup('<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">')
    >>> new_soup
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    >>> type(new_soup)
    <class 'BeautifulSoup.BeautifulSoup'>
    >>> 
    

    相关代码,links很多,所以创建link tag语句需要在for循环里面

        for link in links:
            new_soup = BeautifulSoup('<link rel="stylesheet" href="%s" type="text/css">'%link)
            self.soup.head.insert(0, new_soup)
        self.update_document()
    

    [编辑 2] BeautifulSoup将link标签插入html:

    演示

    >>> from BeautifulSoup import BeautifulSoup
    # Parser content by BeautifulSoup.
    >>> soup = BeautifulSoup("<html><head></head><body></body></html>")
    >>> soup
    <html><head></head><body></body></html>
    # Create New tag.
    >>> new_tag = BeautifulSoup('<link rel="stylesheet" href="css/bootstrap.min.css"/>')
    >>> new_tag
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    # Insert created New tag into head tag i.e. first child of head tag.
    >>> soup.head.insert(0,new_tag)
    >>> soup
    <html><head><link rel="stylesheet" href="css/bootstrap.min.css" /></head><body></body></html>
    >>> new_tag = BeautifulSoup('<link rel="stylesheet" href="css/custom1.css"/>')
    >>> new_tag
    <link rel="stylesheet" href="css/custom1.css" />
    >>> soup.head.insert(0,new_tag)
    >>> soup
    <html><head><link rel="stylesheet" href="css/custom1.css" /><link rel="stylesheet" href="css/bootstrap.min.css" /></head><body></body></html>
    >>> 
    

    [编辑 3]

    我认为您是从 bs4 模块导入的 BeautifulSoup

    BeautifulSoup 是类,它以 html 内容作为参数。

    创建新标签:

    使用BeautifulSoup类的new_tag方法创建新标签。

    使用new_tagattrs 属性添加classhref 属性及其值。

    演示

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup("<html><head></head><body></body></html>")
    >>> soup
    <html><head></head><body></body></html>
    >>> new_link =  soup.new_tag("link")
    >>> new_link
    <link/>
    >>> new_link.attrs["href"] = "custom1.css"
    >>> new_link
    <link href="custom1.css"/>
    >>> soup.head.insert(0, new_link)
    >>> soup
    <html><head><link href="custom1.css"/></head><body></body></html>
    

    【讨论】:

    • 你说得对,但我不得不改成self.soup.head.insert(0, new_soup.find("link")),因为它也插入了“html”标签。
    • @RederickDeathwill:我更新了答案,你能检查它对你有用吗?
    • @RederickDeathwill:再次更新答案,我认为更新的内容对您有用。 :)
    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多