多くの言語でクロールできますが、python に基づくクローラーはより簡潔で便利です。クローラーも Python 言語の重要な部分になっています。クローラーがデータを解析する方法は数多くありますが、前回の記事ではクローラーが取得できるデータの種類と具体的な解析方法について紹介しました。 JSON 解析を行っています。 JSON
Json は JavaScript における単なるオブジェクトと配列であるため、これら 2 つの構造体はオブジェクトと配列です。これら 2 つの構造により、さまざまな複雑な構造を表現できます。
オブジェクト:
オブジェクトは、js では { } で囲まれた内容として表現され、データ構造は { key: value, key: value, ..キーと値のペアの構造 オブジェクト指向言語では、キーがオブジェクトの属性、値がそれに対応する属性値なので理解しやすいです 属性を取得するための値メソッドは object.key ですvalue. この属性値のタイプは、数値、文字列、配列、オブジェクトです。
配列:
js の配列は角括弧 [ ] で囲まれた内容で、データ構造は ["Python", "javascript", "C] ", ...]、値の取得方法はすべての言語と同じで、インデックスを使用して取得します。フィールド値の型は数値、文字列、配列、オブジェクトです。
JSON (JavaScript Object Notation) は、読み書きを容易にする軽量のデータ交換形式です。また、機械の分析と生成も容易になります。 Web サイトのフロントエンドとバックエンド間のデータ対話などのデータ対話シナリオに適しています。 import json
def resolveJson(path):
file = open(path, "rb")
fileJson = json.load(file)
field = fileJson["field"]
futures = fileJson["futures"]
type = fileJson["type"]
name = fileJson["name"]
time = fileJson["time"]
return (field, futures, type, name, time)
def output():
result = resolveJson(path)
print(result)
for x in result:
for y in x:
print(y)
path = r"C:\Users\dell\Desktop\kt\test.json"
output()
関数が複数の値を返す場合はタプルを返すことに注意してください;
文字列に対して for ループを実行する場合、各文字が走査されます
Python JSON
この章では、Python 言語を使用して JSON オブジェクトをエンコードおよびデコードする方法を紹介します。
JSON (JavaScript Object Notation) は、人間が読み書きしやすい軽量のデータ交換形式です。
JSON 関数
JSON 関数を使用するには、json ライブラリをインポートする必要があります: import json。
関数Python オブジェクトにデコードされたエンコードされた JSON 文字列
json.dumps##json.dumps Python オブジェクトを JSON 文字列にエンコードするために使用されます。
構文json.dumps(obj、skipkeys=False、ensure_ascii=True、check_circular=True、allow_nan=True、cls=None、indent=None、区切り文字=None、encoding="utf-8"、default=None、sort_keys=False、**kw)
例
#!/usr/bin/python import json data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = json.dumps(data) print json上記のコードの実行結果は次のとおりです:
[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]パラメータを使用して出力用の JSON データをフォーマットします:
>>> import json >>> print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': ')) { "a": "Runoob", "b": 7 }
Python オリジナルの変換制御type から json type テーブル:
Python#list, tuple array
str, unicodeロング、フロートfalse
None null
json.loads
json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。
语法
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
实例
以下实例展示了Python 如何解码 JSON 对象:
<pre class="brush:php;toolbar:false"> #!/usr/bin/python import json jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = json.loads(jsonData) print text
以上代码执行结果为:
{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
json 类型转换到 python 的类型对照表:
JSON Python
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
使用第三方库:Demjson
Demjson 是 python 的第三方模块库,可用于编码和解码 JSON 数据,包含了 JSONLint 的格式化及校验功能。
Github 地址:https://github.com/dmeranda/demjson
官方地址:http://deron.meranda.us/python/demjson/
环境配置
在使用 Demjson 编码或解码 JSON 数据前,我们需要先安装 Demjson 模块。本教程我们会下载 Demjson 并安装:
$ tar -xvzf demjson-2.2.3.tar.gz $ cd demjson-2.2.3 $ python setup.py install
JSON 函数
函数 描述
encode 将 Python 对象编码成 JSON 字符串
decode 将已编码的 JSON 字符串解码为 Python 对象
encode
Python encode() 函数用于将 Python 对象编码成 JSON 字符串。
语法
demjson.encode(self, obj, nest_level=0)
实例
以下实例将数组编码为 JSON 格式数据:
<pre class="brush:php;toolbar:false"> #!/usr/bin/python import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json
以上代码执行结果为:
[{"a":1,"b":2,"c":3,"d":4,"e":5}]
decode
Python 可以使用 demjson.decode() 函数解码 JSON 数据。该函数返回 Python 字段的数据类型。
语法
demjson.decode(self, txt)
实例
以下实例展示了Python 如何解码 JSON 对象:
<pre class="brush:php;toolbar:false"> #!/usr/bin/python import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print text
以上代码执行结果为:
{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
以上がクローラーの解析方法 1: JOSN 解析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

メモ帳++7.3.1
使いやすく無料のコードエディター

ホットトピック









