【问题标题】:I am unable to understand the meaning of this line of code while working with Beautiful Soup module在使用 Beautiful Soup 模块时,我无法理解这行代码的含义
【发布时间】:2021-05-08 11:15:44
【问题描述】:
def search(self, topic, site):
        bs = self.getPage(site.searchurl + topic)
        searchresults = bs.select(site.resultingList)
        for result in searchresults:
            url = result.select(site.resulturl)[0].attrs["href"]
            if (site.absoluteUrl):
                bs = self.getPage(url)
            else:
                bs = self.getPage(site.url + url)
            if bs is None:
                print("Something was wrong with that page or URL. Skipping!")
                return
            title = self.safeGet(bs, site.titleTag)
            body = self.safeGet(bs, site.bodyTag)
            if title != '' and body != '':
                content = Content(topic, title, body, url)
                content.print()

在这段代码中是什么意思:

result.select(site.resulturl)[0].attrs["href"]

更具体地说,我无法理解attrs["href"]

【问题讨论】:

    标签: python beautifulsoup tags


    【解决方案1】:

    attrs["href"]result.select(site.resulturl)[0] 中提取“href”属性。因此,site.resulturl 很可能包含至少 1 个或多个 <a ... href="..."> 标记(或任何其他具有“href”属性的标记),并且该行将链接从列表中的第一个拉出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2018-02-21
      相关资源
      最近更新 更多