【问题标题】:How can I extract the values from a dictionary returned from my beautifulsoup results?如何从 beautifulsoup 结果返回的字典中提取值?
【发布时间】:2016-11-19 15:07:26
【问题描述】:

我正在开发一个使用 beatifulsoup、Python、requests 和 django 的应用程序。我一直在掌握如何使用美丽的汤。但是,向下钻取似乎有时会混淆不同的元素。我创建了一个功能,虽然不是最好的,它从帖子中抓取链接并使用这些链接转到帖子详细信息页面。并从该页面抓取包含 Facebook 网址和与之关联的图像的脚本数据。这是代码

来自我的 scraper.py

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]
        return entries

    entries = [{'text': div.h4.text,
                'href': div.a.get('href'),
                'tube': youtube_link(div.a.get('href')),
                } for div in video_row][:3]

    return entries

从我的观点.py

   pan = panties()
    context = {
        'pan': pan,
    }
    return render(request, 'index.html', context)

在我的模板中

{% for p in pan %}
   Title: {{p.text}}<br>
   Href: {{p.href}}<br>
   Tube: {{p.tube}}<hr>
{% endfor %}

这是它返回的内容

Title: Juanka - Esperando por ti (Official Video)
Href: http://www.videos.com/video/2962/juanka-esperando-por-ti-official-video-/
Tube: {'text': '<script type="text/javascript">jwplayer("video-setup").setup({file:"http://www.youtube.com/watch?v=QL4JFUHd71o",image:"http://i1.ytimg.com/vi/QL4JFUHd71o/maxresdefault.jpg",primary:"html5",stretching:"fill","controlbar":"bottom",width:"100%",aspectratio:"16:9",autostart:"true",logo:{file:"http://www.panvideos.com/uploads/gopcds-png5787dbcd53a72.png",position:"bottom-right",link:"http://www.panvideos.com/"},sharing:{link:"http://www.panvideos.com/video/2962/juanka-esperando-por-ti-official-video-/","sites":["facebook","twitter","linkedin","pinterest","tumblr","googleplus","reddit"]}});</script>'}

我只想要

http://www.youtube.com/watch?v=QL4JFUHd71o

http://i1.ytimg.com/vi/QL4JFUHd71o/maxresdefault.jpg

分别是视频和图像。我怎样才能做到这一点。我的代码不是一成不变的,我不介意更改它以使其工作。感谢我提前提出的任何建议。

【问题讨论】:

    标签: python json django beautifulsoup


    【解决方案1】:

    如果我理解得很好,您想从您的 p.tube BeautifulSoup 对象中找到 2 个元素。为了便于理解,我将其称为soup

    首先,我会用soup.text 函数摆脱&lt;script&gt;

    然后我会使用正则表达式重新包https://docs.python.org/2/library/re.html找到.setup(来摆脱它之前的所有内容,并使用-2摆脱最后的);

    import re
    s = re(".setup(", soup)
    soup = soup[s.end:-2]
    

    然后,要将您的字符串转换为字典,我建议您使用 ast.literal_evalConvert a String representation of a Dictionary to a dictionary?

    不幸的是,(这很容易)您的字符串格式不正确,无法轻松转换为字典。

    因此,我将摆脱 {} ,并以逗号分隔 ,

    soup = soup[1:-1]
    l = soup.split(',')
    

    希望,因为您正在搜索的元素是前两个,所以您应该很容易找到它们

    【讨论】:

    • 您好,感谢您的回复。但是我如何使它适合我的代码。您的解决方案无法按照您用我的代码解释的方式工作,因为您定义的 p.tube 或汤在模板中。所以我不能这样做 import re s = re(".setup(", soup) soup = soup[s.end:-2]
    • 你能把你拥有的东西加到我的代码上吗?因为我不知道从哪里开始。当我尝试使用 s = re(".setup(", soup) 我得到一个“re is not callable”错误
    • 查看我之前的回复
    • 我想我看到了@Alby 的错误,你有 re 而不是 re.search 或 re.compile 我认为你不能单独使用 re()。在文档中,re 后面总是跟 function
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 2016-03-25
    • 2013-03-05
    相关资源
    最近更新 更多