繁体   English   中英

我如何将所有循环变量存储在 python 的字典中以在 django 中使用

[英]how do i store all loop variables in dictionary of python to use in django

PYTHON

import requests
import re
import os
import csv
from bs4 import BeautifulSoup

headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
}
searche=['note 8 pro','note 9 pro','boat','realme','poco']
for d in searche:
    truelink = d.replace(" ","-")
    truelinkk=('https://www.olx.in/hyderabad_g4058526/q-'+truelink+'?isSearchCall=true')
    r = requests.get(truelinkk,headers=headers).text
    soup=BeautifulSoup(r,'lxml')
    mobile=soup.find_all('li',class_='EIR5N')
    for  note9maxpro in mobile:
        mobi= note9maxpro.find('span',class_='_2tW1I').text
        price= note9maxpro.find('span',class_='_89yzn').text
        date= note9maxpro.find('span',class_='zLvFQ').text
        link=note9maxpro.a.get('href')
        orglink='https://www.olx.in'+link
        price = price.replace("₹ ","")

如何将所有循环变量存储在 python 的字典中以便稍后在 django 中使用我想在 django 中使用此数据作为。 请给我解决方案以减少我的代码行提前谢谢

for i in data:
 i.mobi
 i.price
 i.date

只是 append 他们给一个列表?

for d in searche:
    # (your code here)    

    data = list()
    
    for note9maxpro in mobile:
        element = dict()
        element["mobi"] = note9maxpro.find('span',class_='_2tW1I').text
        element["price"] = note9maxpro.find('span',class_='_89yzn').text
        # and so on
        data.append(element)
Context = {}
 i = 0

 for item in data:
    Context[f”item{i}”] = item.attribute
    i++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM