我正在尝试提取 Soundcloud 页面上“背景图像”的 URL(例如 https://soundcloud.com/ohwondermusic/drive)。我不太确定为什么与从我找到在线指南的其他网页中提取 url 相比,我发现这如此困难。
在我链接的示例网页中,我想要这个 url:https://i1.sndcdn.com/artworks-000125017075-di2n0i-t500x500.jpg可以通过在 Chrome 浏览器中右键单击专辑封面并选择“检查元素”来找到它。
我也想通过某种方式始终如一地为其他 Soundcloud 页面执行此操作(即获取可通过检查专辑插图找到的 URL,该 URL 以 500x500.jpg 结尾)。
有人知道怎么做吗?
编辑:我已经使用各种代码尝试解决问题,大致如下:
def pull2(url):
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)
readOnly = soup.body.find_all('div', attrs={'class': 'image readOnly customImage'})
print readOnly.attrs['style']
或
def test(url):
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)
imgs = soup.findAll("div", {"class":"thumb-pic"})
for img in imgs:
print img.a['href'].split("imgurl=")[1]
最佳答案
看起来你应该能够从每个页面上的正确跨度中获取样式,如下所示:
soup.find("span", class_="sc-artwork")['style']
然后,编写一个正则表达式从中提取 url 或将其拆分为“url”
关于python - 使用 BeautifulSoup/Python 提取网站背景图像的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775358/