Traceback (most recent call last):
File "amazonscraper_advanced.py", line 43, in <module>
cursor.execute(sql)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1136, "Column count doesn't match value count at row 1")
并在页面的前面插入{% assign member = site.data.members[page.author] %}
,则可以显示数据 。如果指定author: valuehere
== Members.yml
{% for member in site.data.members %} ~~~ {{ member.name }} ~~~ {% endfor %}
我已经尝试过像这样重新格式化数据文件:
attorney1:
birth_country: "United States of America"
birth_city: "Paso Robles"
birth_region: CA
birth_zip: 93446
birth_date: "05/1968"
education: "Southeastern University: B.A. History – 2008, University of Florida Levin College of Law: Juris Doctor – 2001"
image: attorney1.jpg
nationality: "United States of America"
name: "Attorney One Esq"
first_name: "Attorney"
last_name: "One"
honorary: Esquire
email: email@example.com
home_country: "United States of America"
home_city: "Ocala"
home_region: "FL"
home_zip: "34482"
gender: Male
permalink: "/lawyers/attorney1.html"
ext: "02"
practices: "Personal Injury · Insurance Litigation"
web: "Lawyer One Esq is a past member of the Hillsborough County Bar Association and Young Lawyers Division, the Lakeland Bar Association, and Emerge. Jon was also served on the Board of Directors for Tri-County Human services, which serves Polk, Hardee, and Highlands counties. Lawyer One Esq is currently a member of the Jacksonville Bar Association."
然后像这样重新编码作者页面:
- author: attorney1
name: "Attorney One"
~~~
目标是能够使用for循环并提取作者页面的数据。如果我将数据文件格式化为:
---
layout: attorney
title: "Attorney One"
crumbtitle: "Attorney One"
permalink: "/lawyers/attorney1.html"
jsontype: lawyer
genre: Law
headline: "Affordable Marion County Legal Representation"
author: attorney1
---
{% assign author = site.data.members | where: "author", "{{page.author}}" %}
<!-- Main -->
<article id="main">
<header class="special container">
<span class="icon fas fa-user-circle"></span>
<h2>About {{ author.name }}</h2>
{{ author.web | markdownify }}
</header>
<!-- One -->
作者页面与attorney1:
name: "Attorney one"
配合使用并中断for循环。
答案 0 :(得分:2)
要在给定列表中成功迭代,您需要的只是结构正确的数据。
要使{% for member in site.data.members %}
正确循环,site.data.members
必须是成员的数组。但是从您发布的信息来看,看起来结果数据是键值对的Hash
(或 dictionary ),而不是Array
。
要确认,您可以先简单地“检查”数据。将以下代码段插入模板中,以获取数据的JSON表示形式:
<pre>
{{ site.data.members | inspect }}
</pre>
要成功进行迭代,生成的JSON 应该以方括号([
,]
)开头和结尾:
[
{
"attorney1": {
"birth_country": "United States of America",
"birth_city": "Paso Robles"
}
},
{
"attorney2": {
"birth_country": "United States of America",
"birth_city": "Paso Robles"
}
},
]
但是,相反,您的members.yml
会产生类似以下内容:
{
"attorney1": {
"birth_country": "United States of America",
"birth_city": "Paso Robles"
},
"attorney2": {
"birth_country": "United States of America",
"birth_city": "Paso Robles"
}
}
如果您希望将所有律师信息包含在一个YAML文件中,则结构为:
# _data/members.yml
- attorney1:
birth_country: "United States of America"
birth_city: "Paso Robles"
- attorney2:
birth_country: "United States of America"
birth_city: "Paso Robles"
或者如果您想单独组织个人信息:
# _data/members/attorney1.yml
birth_country: "United States of America"
birth_city: "Paso Robles"
# _data/members/attorney2.yml
birth_country: "United States of America"
birth_city: "Paso Robles"
要基于给定键选择特定的数据集,可以将数据和键传递到where
过滤器和first
或last
过滤器:
{% assign member = site.data.members | where: 'author', page.author | first %}
使用上述方法,将首先生成另一个成员数组,其中member.author
等于page.author
,然后通过first
过滤器提取第一个条目。