import scrapy
from ..items import CentriItem
from scrapy_playwright.page import PageMethod
class NikespiderSpider(scrapy.Spider):
name = 'nikespider'
def start_requests(self):
url = "https://www.nike.com/ch/en/w/mens-nik1?q=shirts"
yield scrapy.Request(url, meta=dict(
playwright=True,
playwright_include_page=True,
playwright_page_methods=[
PageMethod("wait_for_selector", ".product-card__link-overlay"),
PageMethod("evaluate", "window.scrollBy(0, document.body.scrollHeight)"),
PageMethod("wait_for_selector",".css-c2ovjx:nth-child(25) .product-card__link-overlay"),
]
))
async def parse(self, response):
page = response.meta["playwright_page"]
await page.close()
i = 0
for link in response.css(".product-card__link-overlay::attr(href)"):
i = i + 1
print(i)
async def parse_items(self, response,link):
pass
This is my code for scraping Nike. The url which I am scraping has infinite scrolling. It loads 24 items at first and after you scroll down it loads 24 more. I am trying to add this functionality using scrapy-playwright but it is not working. Anyone able to help me?
It looks like it would probably be easier to extract the data from their api.
import scrapy
class NikespiderSpider(scrapy.Spider):
name = 'nikespider'
def start_requests(self):
for i in range(1, 20):
v = i * 12
url = f"https://api.nike.com/cic/browse/v2?queryid=products&anonymousId=699ECBF06AACCD31DCB69E0C71964748&country=ch&endpoint=%2Fproduct_feed%2Frollup_threads%2Fv2%3Ffilter%3Dmarketplace(CH)%26filter%3Dlanguage(en-GB)%26filter%3DemployeePrice(true)%26filter%3DattributeIds(0f64ecc7-d624-4e91-b171-b83a03dd8550)%26searchTerms%3Dshirts%26anchor%3D{v}%26consumerChannelId%3Dd9a5bc42-4b9c-4976-858a-f159cf99c647%26count%3D24&language=en-GB&localizedRangeStr=%7BlowestPrice%7D%E2%80%94%7BhighestPrice%7D"
yield scrapy.Request(url=url)
def parse(self, response):
yield response.json()
output for the first page looks like this:
{
"data": {
"products": {
"errors": null,
"products": [
{
"cardType": "default",
"cloudProductId": "a44f2253-c745-5143-bd1e-f2c841da1550",
"colorDescription": "Obsidian/Pepper Red/Gorge Green/Gold Dart",
"colorways": [
{
"colorDescription": "Obsidian/Pepper Red/Gorge Green/Gold Dart",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/a77364ff-d0e8-4eaa-83eb-2a206896b28d/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/e1374e74-f614-40dd-aecd-ac4c0bd1a5bb/portugal-french-terry-football-hoodie-rF3Vcw.png"
},
"pdpUrl": "{countryLang}/t/portugal-french-terry-football-hoodie-rF3Vcw/DH4826-451",
"price": {
"currency": "CHF",
"currentPrice": 109.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 109.95,
"minimumAdvertisedPrice": null
},
"cloudProductId": "a44f2253-c745-5143-bd1e-f2c841da1550",
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNew": true,
"label": "JUST_IN",
"pid": "14061770",
"prebuildId": null,
"productInstanceId": null
}
],
"customizable": false,
"hasExtendedSizing": false,
"id": "96633291-36b7-4a6d-a1ab-ef0e264da2c2",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/a77364ff-d0e8-4eaa-83eb-2a206896b28d/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/e1374e74-f614-40dd-aecd-ac4c0bd1a5bb/portugal-french-terry-football-hoodie-rF3Vcw.png"
},
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isGiftCard": false,
"isJersey": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNBA": false,
"isNFL": false,
"isSustainable": false,
"label": "JUST_IN",
"nbyColorway": null,
"pid": "14061770",
"prebuildId": null,
"price": {
"currency": "CHF",
"currentPrice": 109.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 109.95,
"minimumAdvertisedPrice": null
},
"productInstanceId": null,
"productType": "APPAREL",
"properties": null,
"salesChannel": ["NikeApp", "Nike.com", "Nike Store Experiences"],
"subtitle": "Men's Nike French Terry Football Hoodie",
"title": "Portugal",
"url": "{countryLang}/t/portugal-french-terry-football-hoodie-rF3Vcw/DH4826-451"
},
{
"cardType": "default",
"cloudProductId": "a05bd5c6-290e-5d8c-b583-9c8fd49fee3b",
"colorDescription": "Coastal Blue/Green Spark/Dynamic Yellow",
"colorways": [
{
"colorDescription": "Coastal Blue/Green Spark/Dynamic Yellow",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/af0e8a3f-7f4b-417c-b7b1-6acd09607ee2/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/d6150c37-9086-4b1a-b1f0-66e59f3258cb/brazil-travel-fleece-football-hoodie-H4PKQk.png"
},
"pdpUrl": "{countryLang}/t/brazil-travel-fleece-football-hoodie-H4PKQk/DH4822-490",
"price": {
"currency": "CHF",
"currentPrice": 109.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 109.95,
"minimumAdvertisedPrice": null
},
"cloudProductId": "a05bd5c6-290e-5d8c-b583-9c8fd49fee3b",
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNew": true,
"label": "JUST_IN",
"pid": "14061554",
"prebuildId": null,
"productInstanceId": null
}
],
"customizable": false,
"hasExtendedSizing": false,
"id": "9ba8eaff-d94d-445a-b569-8bae4c1d741b",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/af0e8a3f-7f4b-417c-b7b1-6acd09607ee2/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/d6150c37-9086-4b1a-b1f0-66e59f3258cb/brazil-travel-fleece-football-hoodie-H4PKQk.png"
},
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isGiftCard": false,
"isJersey": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNBA": false,
"isNFL": false,
"isSustainable": false,
"label": "JUST_IN",
"nbyColorway": null,
"pid": "14061554",
"prebuildId": null,
"price": {
"currency": "CHF",
"currentPrice": 109.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 109.95,
"minimumAdvertisedPrice": null
},
"productInstanceId": null,
"productType": "APPAREL",
"properties": null,
"salesChannel": ["NikeApp", "Nike.com", "Nike Store Experiences"],
"subtitle": "Men's Fleece Football Hoodie",
"title": "Brazil Travel",
"url": "{countryLang}/t/brazil-travel-fleece-football-hoodie-H4PKQk/DH4822-490"
},
{
"cardType": "default",
"cloudProductId": "40d88287-0129-52c7-ad5e-646ba8333389",
"colorDescription": "Game Royal/Blue Fury/Blue Fury",
"colorways": [
{
"colorDescription": "Game Royal/Blue Fury/Blue Fury",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/b66e9663-2fd1-4398-9270-d4e4d46ed83e/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/873eaab7-cfec-4b70-b6af-19880a563637/england-strike-dri-fit-short-sleeve-football-top-1m2GSc.png"
},
"pdpUrl": "{countryLang}/t/england-strike-dri-fit-short-sleeve-football-top-1m2GSc/DH6443-480",
"price": {
"currency": "CHF",
"currentPrice": 59.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 59.95,
"minimumAdvertisedPrice": null
},
"cloudProductId": "40d88287-0129-52c7-ad5e-646ba8333389",
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNew": true,
"label": "JUST_IN",
"pid": "14062596",
"prebuildId": null,
"productInstanceId": null
}
],
"customizable": false,
"hasExtendedSizing": false,
"id": "f73148f5-4b63-4b1f-84cc-b03bcc408ca1",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/b66e9663-2fd1-4398-9270-d4e4d46ed83e/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/873eaab7-cfec-4b70-b6af-19880a563637/england-strike-dri-fit-short-sleeve-football-top-1m2GSc.png"
},
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isGiftCard": false,
"isJersey": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNBA": false,
"isNFL": false,
"isSustainable": true,
"label": "JUST_IN",
"nbyColorway": null,
"pid": "14062596",
"prebuildId": null,
"price": {
"currency": "CHF",
"currentPrice": 59.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 59.95,
"minimumAdvertisedPrice": null
},
"productInstanceId": null,
"productType": "APPAREL",
"properties": null,
"salesChannel": ["NikeApp", "Nike.com", "Nike Store Experiences"],
"subtitle": "Men's Nike Dri-FIT Short-Sleeve Football Top",
"title": "England Strike",
"url": "{countryLang}/t/england-strike-dri-fit-short-sleeve-football-top-1m2GSc/DH6443-480"
},
{
"cardType": "default",
"cloudProductId": "e04c4bb3-4382-5df4-9f03-7c1d393f9d05",
"colorDescription": "Obsidian/Green Glow/Green Glow",
"colorways": [
{
"colorDescription": "Obsidian/Green Glow/Green Glow",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/4cbd4672-d5ac-453b-b023-2d5160b33341/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/7f04a8ed-649c-4705-b110-0c605b852f7b/australia-2022-23-stadium-away-dri-fit-football-shirt-XKZCLs.png"
},
"pdpUrl": "{countryLang}/t/australia-2022-23-stadium-away-dri-fit-football-shirt-XKZCLs/DN0676-451",
"price": {
"currency": "CHF",
"currentPrice": 119.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 119.95,
"minimumAdvertisedPrice": null
},
"cloudProductId": "e04c4bb3-4382-5df4-9f03-7c1d393f9d05",
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNew": true,
"label": "JUST_IN",
"pid": "14063595",
"prebuildId": null,
"productInstanceId": null
}
],
"customizable": false,
"hasExtendedSizing": false,
"id": "0027a0c6-0e97-445c-b6ed-a92a6bfdf18b",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/4cbd4672-d5ac-453b-b023-2d5160b33341/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/7f04a8ed-649c-4705-b110-0c605b852f7b/australia-2022-23-stadium-away-dri-fit-football-shirt-XKZCLs.png"
},
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isGiftCard": false,
"isJersey": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNBA": false,
"isNFL": false,
"isSustainable": true,
"label": "JUST_IN",
"nbyColorway": null,
"pid": "14063595",
"prebuildId": null,
"price": {
"currency": "CHF",
"currentPrice": 119.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 119.95,
"minimumAdvertisedPrice": null
},
"productInstanceId": null,
"productType": "APPAREL",
"properties": null,
"salesChannel": ["NikeApp", "Nike.com", "Nike Store Experiences"],
"subtitle": "Men's Nike Dri-FIT Football Shirt",
"title": "Australia 2022/23 Stadium Away",
"url": "{countryLang}/t/australia-2022-23-stadium-away-dri-fit-football-shirt-XKZCLs/DN0676-451"
},
{
"cardType": "default",
"cloudProductId": "758da5f5-ea79-5bf7-a401-8ccb945a5f16",
"colorDescription": "Off-Noir/White",
"colorways": [
{
"colorDescription": "Off-Noir/White",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/1efdbff3-1c9e-4984-9bf2-8da11d8f5161/image.jpg",
"squarishURL": "https://static.nike.com/a/images/t_default/5ca1f9a9-f1fa-42fe-a552-571ffbcc8ba6/nikecourt-dri-fit-rafa-challenger-short-sleeve-tennis-top-wNcD3N.png"
},
"pdpUrl": "{countryLang}/t/nikecourt-dri-fit-rafa-challenger-short-sleeve-tennis-top-wNcD3N/DD8547-045",
"price": {
"currency": "CHF",
"currentPrice": 84.95,
"discounted": false,
"employeePrice": 0,
"fullPrice": 84.95,
"minimumAdvertisedPrice": null
},
"cloudProductId": "758da5f5-ea79-5bf7-a401-8ccb945a5f16",
"inStock": true,
"isComingSoon": false,
"isBestSeller": false,
"isExcluded": false,
"isLaunch": false,
"isMemberExclusive": false,
"isNew": true,
"label": "JUST_IN",
"pid": "14168043",
"prebuildId": null,
"productInstanceId": null
},
{
"colorDescription": "Pure Platinum/Pink Gaze/Black",
"images": {
"portraitURL": "https://static.nike.com/a/images/c_limit,w_400,f_auto/t_product_v1/dbf2edc2-da8b-4e7e-976f-5d16f207b449/image.jpg",
...
Related
I currently have data stored in cosmosdb fetchable like so
SELECT t.code, t.departures from t
which returns like
{
"code": "BAHVB2",
"departures": [
{
"code": "14601",
"startDate": "2022-09-13T00:00:00",
"endDate": "2022-09-20T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14602",
"startDate": "2022-09-27T00:00:00",
"endDate": "2022-10-04T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14603",
"startDate": "2022-12-10T00:00:00",
"endDate": "2022-12-17T00:00:00",
"available": true,
"definite": false,
}
]
},
{
"code": "BBFLL2",
"departures": [
{
"code": "14618",
"startDate": "2022-08-30T00:00:00",
"endDate": "2022-09-04T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14619",
"startDate": "2022-09-09T00:00:00",
"endDate": "2022-09-14T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14620",
"startDate": "2022-09-19T00:00:00",
"endDate": "2022-09-24T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14622",
"startDate": "2022-10-11T00:00:00",
"endDate": "2022-10-16T00:00:00",
"available": true,
"definite": false,
},
{
"code": "14623",
"startDate": "2022-11-07T00:00:00",
"endDate": "2022-11-12T00:00:00",
"available": true,
"definite": false,
}
]
},
]
How would I create a query to order the results based on the first/minimum startDate ?
as in code: BAHVB2 has the minimum 2022-09-13, and BBFLL2 has the minimum 2022-08-30, so BBFFL2 should appear first?
Is there a good way to perform where clauses on this as well? I am at a crossroads as every guide ive seen so far isnt so explanatory
Something like this should do the job
SELECT t.code, t.departures
FROM t
ORDER BY MIN(ARRAY(SELECT d.startDate FROM d IN t.departures))
I'm trying to webscrape the data table from this site:
https://fl511.com/list/events/traffic?start=0&length=25&filters%5B0%5D%5Bi%5D=5&filters%5B0%5D%5Bs%5D=Incidents&order%5Bi%5D=8&order%5Bdir%5D=asc
But unfortunately, when I print out the table it doesn't return the tbody tag (which the information is stored in). All the other tags are shown. Is there a workaround to this?
url = Request(
url,
headers={'User-Agent': 'Mozilla/5.0'}
)
webpage = urlopen(url).read()
table = soup.find_all('table')
print(table)
The data is loaded from external source via Javascript. You can use this example how to load the data:
import json
import requests
data = {
"draw": 1,
"columns": [
{
"data": None,
"name": "",
"searchable": False,
"orderable": False,
"search": {"value": "", "regex": False},
"title": "",
"visible": True,
"isUtcDate": False,
"isCollection": False,
},
{
"data": "region",
"name": "region",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "county",
"name": "county",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "roadwayName",
"name": "roadwayName",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "direction",
"name": "direction",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "type",
"name": "type",
"searchable": False,
"orderable": True,
"search": {"value": "Incidents", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "severity",
"name": "severity",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "description",
"name": "description",
"searchable": False,
"orderable": False,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "startTime",
"name": "startTime",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": "lastUpdated",
"name": "lastUpdated",
"searchable": False,
"orderable": True,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
{
"data": 10,
"name": "",
"searchable": False,
"orderable": False,
"search": {"value": "", "regex": False},
"isUtcDate": False,
"isCollection": False,
},
],
"order": [{"column": 8, "dir": "asc"}],
"start": 0,
"length": 25,
"search": {"value": "", "regex": False},
}
url = "https://fl511.com/List/GetData/traffic"
data = requests.post(url, json=data).json()
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
for i, d in enumerate(data["data"], 1):
print(i, d["description"])
print()
print("Records total:", data["recordsTotal"])
print("Records filtered:", data["recordsFiltered"])
Prints:
1 Crash in Highlands County on US-27 South, at Lake Josephine Dr. Right lane blocked. Last updated at 04:24 PM.
2 Emergency vehicles in Highlands County on US-27 North, at Lake Josephine Dr. Right lane blocked. Last updated at 04:25 PM.
3 Crash in Manatee County on US-41 North, at Pearl Ave. All lanes blocked. Last updated at 04:29 PM.
4 Crash in Polk County on I-4 East, beyond CR-557. 2 Left lanes blocked. Last updated at 04:32 PM.
5 Emergency vehicles in Manatee County on US-41 South, at Pearl Ave. Left lane blocked. Last updated at 04:35 PM.
6 Crash in Miami-Dade County on I-195 East, beyond North Miami Ave. Right lane blocked. Last updated at 05:03 PM.
7 Crash in Santa Rosa County on I-10 East, ramp to Exit 22 (SR-281/Avalon Blvd). Right shoulder blocked. Last updated at 05:05 PM.
8 Emergency vehicles in Santa Rosa County on I-10 West, at Exit 22 (SR-281/Avalon Blvd). Left shoulder blocked. Last updated at 05:02 PM.
9 Multi-vehicle crash in Duval County on I-295 E South, before Between Atlantic Blvd/St Johns Bluff Rd. Left shoulder blocked. Last updated at 05:30 PM.
Records total: 93
Records filtered: 9
I want to use selenium to login to this webpage: https://betsapi.com/rs/pinnaclesports/2852774/BOOM-ID-v-Neon-Esports
I created this code:
from selenium import webdriver
u = 'https://betsapi.com/rs/pinnaclesports/2852774/BOOM-ID-v-Neon-Esports'
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe")
driver.get(u)
for cookie in cookies:
driver.add_cookie(cookie)
I used the chrome extension 'EditThisCookie' to get the cookies and I store them into list of dictionaries:
cookies =[
{
"domain": ".betsapi.com",
"expirationDate": 1603651291.620966,
"hostOnly": False,
"httpOnly": True,
"name": "__cfduid",
"path": "/",
"secure": False,
"session": False,
"storeId": "0",
"value": "d6db2b7ec158b5623df65e61e0fc709da1601059292",
"id": 1
},
{
"domain": ".betsapi.com",
"expirationDate": 1636449445,
"hostOnly": False,
"httpOnly": False,
"name": "__gads",
"path": "/",
"secure": False,
"session": False,
"storeId": "0",
"value": "ID=c1df2d77d8aeb6b0-22ec198446a60058:T=1602753445:RT=1602753445:S=ALNI_Mb9ZXRFX374-MAx-ZWd8WXHlLbNHA",
"id": 2
},
{
"domain": ".betsapi.com",
"expirationDate": 1665999953,
"hostOnly": False,
"httpOnly": False,
"name": "_ga",
"path": "/",
"secure": False,
"session": False,
"storeId": "0",
"value": "GA1.2.1329605927.1601059303",
"id": 3
},
{
"domain": ".betsapi.com",
"expirationDate": 1603014353,
"hostOnly": False,
"httpOnly": False,
"name": "_gid",
"path": "/",
"secure": False,
"session": False,
"storeId": "0",
"value": "GA1.2.137866329.1602927773",
"id": 4
},
{
"domain": ".betsapi.com",
"hostOnly": False,
"httpOnly": False,
"name": "sid",
"path": "/",
"secure": False,
"session": True,
"storeId": "0",
"value": "7jq0ph5rho4cdat4cngjmdmeg9",
"id": 5
},
{
"domain": "betsapi.com",
"expirationDate": 1614199343,
"hostOnly": True,
"httpOnly": False,
"name": "hstpconfig",
"path": "/",
"secure": True,
"session": False,
"storeId": "0",
"value": "eyJJRCI6IjY2NTE4NDM2dWk1ZjZlMzllOTM3NjQ4IiwiQ1RSIjoiVE4iLCJSZWdpb24iOm51bGwsIkJyb3dzZXIiOiJDaHJvbWUiLCJQbGF0Zm9ybSI6IldpbmRvd3MiLCJNb2JpbGUiOjAsIkJvdCI6MCwicmVtb3RlX2FkZHIiOjMzMDMwODcxOTQsIkxhc3RVcGRhdGUiOjE2MDEwNTkzMDV9",
"id": 6
},
{
"domain": "betsapi.com",
"expirationDate": 1602999953,
"hostOnly": True,
"httpOnly": False,
"name": "hstpcount45424",
"path": "/",
"secure": True,
"session": False,
"storeId": "0",
"value": "eyJDbGljayI6MCwiQ291bnRlciI6MX0%3D",
"id": 7
},
{
"domain": "betsapi.com",
"expirationDate": 1603219304,
"hostOnly": True,
"httpOnly": False,
"name": "lasttrack45424",
"path": "/",
"secure": True,
"session": False,
"storeId": "0",
"value": "1",
"id": 8
},
{
"domain": "betsapi.com",
"hostOnly": True,
"httpOnly": False,
"name": "tz",
"path": "/",
"secure": False,
"session": True,
"storeId": "0",
"value": "Europe%2FBerlin",
"id": 9
}
]
The code open the driver but don't login, how I can login using cookies ?
Hi instead of extension,
use driver.get_cookies() into file and use this file to login again as driver.add_cookie()
or use pickle files
check this
https://intellipaat.com/community/6078/how-to-save-and-load-cookies-using-python-selenium-webdriver
The problem is when I am running the script I am getting the following error:
Traceback (most recent call last): File "test1.py", line 16, in
output.writerow(data[0].keys()) KeyError: 0
Following is the python code:
import csv, json, sys
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput)
outputFile = open(fileOutput, 'w')
data = json.load(inputFile)
inputFile.close()
output = csv.writer(outputFile)
output.writerow(data[0].keys())
for row in data:
output.writerow(row.values())
Formatted JSON data :
{
"response": [
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T11:20:37",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 1,
"raw_message": "A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.",
"forum": "expressandstar",
"createdAt": "2018-11-15T11:20:37",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-233509073ed3432027d48b1a83f5fbd2/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-233509073ed3432027d48b1a83f5fbd2",
"url": "",
"id": "250728493",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:40:52",
"disable3rdPartyTrackers": true,
"name": "baggiebuoy"
},
"parent": null,
"isFlagged": false,
"likes": 1,
"id": "4196155749",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.</p>",
"isEdited": true,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:37:59",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "This could be heaven, or this could be hell.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:37:59",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": 4196013534,
"isFlagged": false,
"likes": 0,
"id": "4196048572",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>This could be heaven, or this could be hell.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:36:50",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "The crappest of crap deals.\nWill never get through Parliament.\n\nYou are the weakest link May, goodbye.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:36:50",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196044068",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>The crappest of crap deals.<br>Will never get through Parliament.</p><p>You are the weakest link May, goodbye.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:28:13",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "We are all just prisoners here of our own device. \ud83d\ude01",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:28:13",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": 4195981074,
"isFlagged": false,
"likes": 0,
"id": "4196013534",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>We are all just prisoners here of our own device. \ud83d\ude01</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:27:03",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Should be no deal end off.\nNobody could possibly be the winner so it's simple leave and that's it .\nTheir will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:27:03",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-90ecce8d5dad4396f681182cb470872c/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-90ecce8d5dad4396f681182cb470872c",
"url": "",
"id": "251694793",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-12T20:20:43",
"disable3rdPartyTrackers": true,
"name": "wanderer in eire"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012501",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Should be no deal end off.<br>Nobody could possibly be the winner so it's simple leave and that's it .<br>Their will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:26:42",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Damned if she does, damned if she doesn't. All for a cause she does not believe in.\nShe will go down in history whatever happens. \nThe question is: who better to lead the country at such an important juncture??\nAnswers on a postcard!",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:26:42",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012237",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Damned if she does, damned if she doesn't. All for a cause she does not believe in.<br>She will go down in history whatever happens. <br>The question is: who better to lead the country at such an important juncture??<br>Answers on a postcard!</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T09:50:32",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.\n\nYou can check out any time you like, but you can never leave.",
"forum": "expressandstar",
"createdAt": "2018-11-15T09:50:32",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-e82a88d937e60267fd2c866b01131ada/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-e82a88d937e60267fd2c866b01131ada",
"url": "",
"id": "250727584",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:27:23",
"disable3rdPartyTrackers": false,
"name": "Olly the cat"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4195981074",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.</p><p>You can check out any time you like, but you can never leave.</p>",
"isEdited": false,
"isApproved": true
}
],
"code": 0,
"cursor": {
"more": false,
"total": null,
"prev": null,
"hasNext": false,
"next": "1542275432008325:1:0",
"id": "1542275432008325:1:0",
"hasPrev": false
}
}
My main project is taking data from the JSON file and making a relational database with the data.
The top-level object in that JSON data is a dictionary. But you are trying to index it as though it were a list. (data[0]). Since 0 is not a key of the dictionary, trying to use it as a key results in a KeyError.
If you want the keys of the top-kevel object, use data.keys().
I'm having trouble iterating through a JSON inside the django template.
My JOSN looks like,
{
"main_cats": [
{
"pk": 1,
"model": "BeeForms.maincategory",
"fields": {
"is_visible": true,
"name": "Test Main",
"order_by_asc": true,
"base_category": 1,
"help_text": "",
"is_seller_field": false,
"position": 10,
"show_seller": true,
"order_by_type": true
}
}
],
"sub_cats": [
{
"pk": 15,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[0-9a-zA-Z]",
"library_id": "TextBox",
"input_length": 255,
"name": "TextBox",
"order_by_asc": false,
"field_type": "text",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": true,
"field_display_type": "input",
"order_by_type": true,
"has_enumerations": false
}
},
{
"pk": 16,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[A-Za-z0-9]",
"library_id": "SelectBox",
"input_length": 255,
"name": "SelectBox",
"order_by_asc": false,
"field_type": "",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "select",
"order_by_type": true,
"has_enumerations": true
}
},
{
"pk": 18,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[0-9a-zA-Z]",
"library_id": "TextArea",
"input_length": 255,
"name": "TextArea",
"order_by_asc": false,
"field_type": "varchar",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "textarea",
"order_by_type": true,
"has_enumerations": false
}
},
{
"pk": 22,
"model": "BeeForms.subcategory",
"fields": {
"regex": "[A-Za-z0-9]",
"library_id": "Checkbox",
"input_length": 255,
"name": "Checkbox",
"order_by_asc": false,
"field_type": "",
"main_category": 1,
"is_active": true,
"is_visible": true,
"help_text": "",
"position": 10,
"is_required": false,
"field_display_type": "checkbox",
"order_by_type": true,
"has_enumerations": true
}
}
],
"base_cats": [
{
"pk": 1,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 10,
"order_by_asc": true,
"name": "Sales Information",
"order_by_type": true
}
},
{
"pk": 2,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 20,
"order_by_asc": true,
"name": "Listing information",
"order_by_type": true
}
},
{
"pk": 3,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 30,
"order_by_asc": true,
"name": "Land & Tax",
"order_by_type": true
}
},
{
"pk": 4,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 40,
"order_by_asc": true,
"name": "Exterior Construction",
"order_by_type": true
}
},
{
"pk": 5,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 50,
"order_by_asc": true,
"name": "Interior/Utilities",
"order_by_type": true
}
},
{
"pk": 6,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 60,
"order_by_asc": true,
"name": "HOA/Community",
"order_by_type": true
}
},
{
"pk": 7,
"model": "BeeForms.basecategory",
"fields": {
"form_type": 1,
"position": 70,
"order_by_asc": true,
"name": "Remarks",
"order_by_type": true
}
}
],
"enum_content": [
{
"pk": 58,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectYesA1",
"name": "SelectYesA1",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 53,
"position": 10,
"order_by_type": true
}
},
{
"pk": 59,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectNoA1",
"name": "SelectNoA1",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 53,
"position": 10,
"order_by_type": true
}
},
{
"pk": 60,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectCheckBoxYes",
"name": "SelectCheckBoxYes",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 54,
"position": 10,
"order_by_type": true
}
},
{
"pk": 61,
"model": "BeeForms.enumerationcontent",
"fields": {
"library_id": "SelectCheckBoxNo",
"name": "SelectCheckBoxNo",
"order_by_asc": false,
"is_visible": true,
"is_active": true,
"enum_type_category": 54,
"position": 10,
"order_by_type": true
}
}
],
"enums": [
{
"pk": 20,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "SelectYes",
"name": "SelectYes",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 16
}
},
{
"pk": 21,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "SelectNo",
"name": "SelectNo",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 16
}
},
{
"pk": 30,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "Yes",
"name": "Yes",
"order_by_asc": false,
"is_visible": true,
"has_type": true,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 22
}
},
{
"pk": 31,
"model": "BeeForms.enumerations",
"fields": {
"library_id": "No",
"name": "No",
"order_by_asc": false,
"is_visible": true,
"has_type": false,
"is_active": true,
"position": 10,
"order_by_type": true,
"sub_category": 22
}
}
],
"enum_types": [
{
"pk": 53,
"model": "BeeForms.enumerationtype",
"fields": {
"regex": "[A-Za-z0-9]",
"is_visible": true,
"name": "SelectYesA",
"library_id": "SelectYesA",
"field_type": "",
"is_active": true,
"has_content": true,
"enum_category": 20,
"field_display_type": "select",
"input_length": 255
}
},
{
"pk": 54,
"model": "BeeForms.enumerationtype",
"fields": {
"regex": "[A-Za-z0-9]",
"is_visible": true,
"name": "",
"library_id": "",
"field_type": "",
"is_active": true,
"has_content": true,
"enum_category": 21,
"field_display_type": "checkbox",
"input_length": 255
}
}
]
}
The code I use to create the json file is as follows.
def serialize(self, instance_name, form, destination):
""" Seralizes the information passed in."""
# we need to make sure that the directory is real.
if not os.path.exists(destination):
os.makedirs(destination)
# Lets grab all the information needed and serailize it all at the same time.
base_cats = serializers.serialize('json', BaseCategory.objects.all().filter(form_type_id=form.id).order_by('id'))
main_cats = serializers.serialize('json', MainCategory.objects.all().order_by('id'))
sub_cats = serializers.serialize('json', SubCategory.objects.all().order_by('id'))
enums = serializers.serialize('json', Enumerations.objects.all().order_by('id'))
enum_types = serializers.serialize('json', EnumerationType.objects.all().order_by('id'))
enum_content = serializers.serialize('json', EnumerationContent.objects.all().order_by('id'))
# now that they are all serailized make it into a list of simplejson
base_list = simplejson.loads(base_cats)
main_list = simplejson.loads(main_cats)
sub_list = simplejson.loads(sub_cats)
enums_list = simplejson.loads(enums)
enum_type_list = simplejson.loads(enum_types)
enum_content_list = simplejson.loads(enum_content)
# now we combine them all together...
self.json_data = simplejson.dumps({'base_cats': base_list, 'main_cats': main_list, 'sub_cats': sub_list,
'enums': enums_list, 'enum_types': enum_type_list,
'enum_content': enum_content_list}, indent=4)
print(self.json_data)
# now that we combined them into json_data. Lets write it out to a file.
with open(destination + instance_name + '.json', 'w') as json_write:
json_write.write(self.json_data)
I load the JSON into a Python Object with the following.
with open(instance_location + '.json', 'r') as json_read:
self.json_data = json_read.read()
# now lets take the data and load into json array
data = simplejson.loads(self.json_data)
# now lets grab each list.
base_cats = data['base_cats']
main_cats = data['main_cats']
sub_cats = data['sub_cats']
enums = data['enums']
enum_types = data['enum_types']
enum_content = data['enum_content']
# lets return it all!
return base_cats, main_cats, sub_cats, enums, enum_types, enum_content
In straight Python I'm able to loop through (for example base_cats) and get the information I need with the following for loop
for base in base_dict:
print base['pk']
print base['fields']['name']
When I try that for loop in the django template I get the error of:
Could not parse the remainder: '['pk']' from 'base['pk']'
With how the JSON is structered how would I loop through it in the django template as the following is not working.
{% for base in base_cats %}
{{ base['pk'] }}
{% endfor %}
Access the fields with the dot operator in the template, not with brackets:
{% for base in base_cats %}
{{ base.pk }}
{% endfor %}
For reference:
https://docs.djangoproject.com/en/1.10/ref/templates/language/#template-variables