【发布时间】:2016-07-18 00:51:14
【问题描述】:
我使用 beutifulsoup 创建了一个函数,它返回看起来像结果字典的内容。但如果我尝试
results['file']
或
results[0]
它没有返回我想要的结果。我想要文件和图像。这是我的代码
def panties():
pan_url = 'http://www.panvideos.com'
html = requests.get(pan_url, headers=headers)
soup = BeautifulSoup(html.text, 'html5lib')
video_row = soup.find_all('div', {'class': 'video'})
def youtube_link(url):
youtube_page = requests.get(url, headers=headers)
soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
video_row = soupdata.find('div', {'class': 'video-player'})
entries = [{'text': str(div),
} for div in video_row][3]['text']
oldstring = str(entries)
removed = '<script type="text/javascript">jwplayer("video-setup").setup('
newstring = oldstring.replace(removed, "")
removed_two = ');</script>'
newstring_two = newstring.replace(removed_two, "")
return newstring_two
entries = [{'text': div.h4.text,
'href': div.a.get('href'),
'tube': youtube_link(div.a.get('href')),
} for div in video_row][:1]
return entries
这是它返回的内容
{file:"http://www.youtube.com/watch?v=jucBuAzuZ0E",image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/bien-png578aab16676e1.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/3020/alejandro-sanz-deja-que-te-bese-ft-marc-anthony-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}}
我真正想要的是
file:"http://www.youtube.com/watchv=jucBuAzuZ0E"
image:"http://i1.ytimg.com/vi/jucBuAzuZ0E/maxresdefault.jpg"
我如何抓住这个?我正在使用 django,我尝试这样做
{% for p in pan %}
Title: {{p.text}}<br>
Href: {{p.href}}<br>
Tube: {{p.tube['file']}}<hr>
{% endfor %}
但我遇到了解析错误。如何访问文件和图像?
【问题讨论】:
标签: python django dictionary