在Python Jupyter中尝试使用.difference()函数时出错



上下文

我目前正在学习一门关于网络抓取的课程。进入抓取javascript模块后,使用函数set_1.difference(set_2)来区分旧变量和新创建的变量。但当我这么做的时候,它出现了一个错误:

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

我在网上搜索,偶然发现了这个网站。但是在他们自己的网站上运行这个例子时出现了一个错误

问题

为什么这不起作用?我想打印新生成的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)

不知道什么是"r〃;对象是,所以无法验证您的代码,但difference是一种集合方法,而不是列表方法。

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

这应该可以做到:set(new_downloads).difference(downloads)

最新更新