我试图用Python从yt-mp3
中提取下载链接,但是我找不到它。在
这是我的代码:
from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
import urllib2
#http://www.yt-mp3.com/watch?v=cXAxpoC8o9w
url = "http://www.yt-mp3.com/watch?v="+"cXAxpoC8o9w"#YT video ID
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(url,headers=hdr)
website = urllib2.urlopen(req)
html = website.read()
soup = BeautifulSoup(html)
links = soup.find_all('a')
for tag in links:
link = tag.get('href',None)
if link is not None:
print link
这个网站的设计是为了让你很难提取你想要的链接文本。因此,使用
urllib2
或request
是没有帮助的。在为了解决这个问题,您需要使用类似
selenium
的方法来自动化webbrowser。在这种情况下,您需要自动将鼠标悬停在下载按钮上。正是这个动作让人们看到了链接。在具体做法如下:
进一步的改进可以去除
sleep()
。在这将显示如下内容:
^{pr2}$相关问题 更多 >
编程相关推荐