【问题标题】:Error whilst trying to use the .difference() function in Python Jupyter尝试在 Python Jupyter 中使用 .difference() 函数时出错
【发布时间】:2021-01-17 21:39:36
【问题描述】:

上下文

我目前正在学习关于网络抓取的课程。在抓取 javascript 模块时,使用函数 set_1.difference(set_2) 将旧变量与新创建的变量区分开来。但是当我这样做时,它带来了这个错误:

AttributeError: 'list' object has no attribute 'difference'

我在网上搜索并偶然发现了这个website。但是在他们自己的网站上运行这个例子却报错了

问题

这不起作用的任何原因?我想打印新生成的 javascript 链接。以下是我尝试运行的代码:

from requests_html import AsyncHTMLSession
session = AsyncHTMLSession()

r = await session.get('https://www.ons.gov.uk/economy/economicoutputandproductivity/output/datasets/economicactivityfasterindicatorsuk')
r.status_code
 

divs = r.html.find('div')
downloads = r.html.find('a')
urls = r.html.absolute_links
    
# Now need to render the javascript. Downloads chromium the first time we use it,
# It is a browser that has no GUI
await r.html.arender()
    
new_divs = r.html.find('div')
new_downloads = r.html.find('a')
new_urls = r.html.absolute_links
    
# Get only the newly created html
new_downloads.difference(downloads)

【问题讨论】:

    标签: javascript python web-scraping python-requests


    【解决方案1】:

    不知道“r”对象是什么,因此无法验证您的代码,但difference 是集合方法,而不是列表。

    https://docs.python.org/3/library/stdtypes.html#frozenset.difference

    这应该可以解决问题:set(new_downloads).difference(downloads)

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2017-03-23
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      相关资源
      最近更新 更多