【问题标题】:render scrape data in browser without storing in database在浏览器中呈现抓取数据而不存储在数据库中
【发布时间】:2019-09-15 18:22:01
【问题描述】:

有什么方法可以在浏览器中呈现抓取数据而不存储在数据库中。

用于抓取数据的代码。

search = query.lower()
p_search = "-".join(search.split())
url = "xyz"+p_search
myurl = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
uReq = ureq(myurl)
uRead = uReq.read()
uReq.close()
soup = bs(uRead, 'lxml')
device_profile = soup.find('div', {'id': 'device-profile'})
return render(request, 'phone/device_profile.html', {'device': device_profile})

在浏览器中呈现数据的代码。

{% if device %}

     {% for row in device %}

      {{ row }}

    {% endfor %}
{% endif %}

【问题讨论】:

  • 那么你的代码有什么问题?
  • 代码运行良好...我只想以 HTML 形式呈现数据...它在浏览器中的显示方式... [

    关于 Oppo Find X

    ,

标签: python django beautifulsoup


【解决方案1】:

所以用你喜欢的 HTML 填充你的模板 ('phone/device_profile.html') 并使用模板。例如:

<html>
<head>
    <title>Some title</title>
</head>
<body>
    <h2 class="h3 blue">About Oppo Find X</h2>
<ul>
    {% for row in device %}
        <li>{{ row }}</li>
    {% endfor %}
</ul>
</body>
</html>

更新:

您需要了解device 的格式。看起来它是一个字符串列表。第二个字符串是您要粘贴到模板中的 HTML 字符串。因此,只需将需要的元素传递给模板,而不是 for 循环:

return render(request, 'phone/device_profile.html', {'device': device_profile[1]})

直接在模板中使用:

<html>
<head>
    <title>Some title</title>
</head>
<body>
    {{ device }}
</body>
</html>

【讨论】:

  • 很遗憾,“不起作用”是一个非常广泛的问题。
  • 只知道异常名称就很难通过 StackOverflow 调试某人的代码。尝试打印device_profile
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 2011-04-22
  • 2013-09-25
  • 1970-01-01
  • 2023-03-07
  • 2016-07-24
  • 2017-01-23
  • 1970-01-01
相关资源
最近更新 更多