语境
我目前正在学习有关网络抓取的课程。进入有关抓取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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句