假设我想抓取以下网址:
https://soundcloud.com/search/sounds?q=edm&filter.created_at=last_week
我有以下python代码:
import requests
from lxml import html
urlToSearch = 'https://soundcloud.com/search/sounds?q=edm&filter.created_at=last_week'
page = requests.get(urlToSearch)
tree = html.fromstring(page.content)
print(tree.xpath('//*[@id="content"]/div/div/div[3]/div/div/div/ul/div/div/text()'))
问题是当我在以下 xpath 打印文本时:
//*[@id="content"]/div/div/div[3]/div/div/div/ul/div/div
什么也没有出现,但[]
尽管我确认“找到 500 多首曲目”应该在那里。我究竟做错了什么?
问题是请求不会生成动态内容。
右击页面并查看页面源代码,您会看到静态内容不包含您在动态内容加载后看到的任何内容。
但是,(使用 Chrome)打开开发工具,点击网络和 XHR。看起来您可以通过 API 获取数据,这总比抓取要好!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句