MCPcopy
hub / github.com/scrapy/scrapy / test_html_has_json

Method test_html_has_json

tests/test_selector.py:146–189  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

144 assert resp.jmespath("content").jmespath("name.age").get() == "18"
145
146 def test_html_has_json(self) -> None:
147 body = """
148 <div>
149 <h1>Information</h1>
150 <content>
151 {
152 "user": [
153 {
154 "name": "A",
155 "age": 18
156 },
157 {
158 "name": "B",
159 "age": 32
160 },
161 {
162 "name": "C",
163 "age": 22
164 },
165 {
166 "name": "D",
167 "age": 25
168 }
169 ],
170 "total": 4,
171 "status": "ok"
172 }
173 </content>
174 </div>
175 """
176 resp = TextResponse(url="http://example.com", body=body, encoding="utf-8")
177 assert resp.xpath("//div/content/text()").jmespath("user[*].name").getall() == [
178 "A",
179 "B",
180 "C",
181 "D",
182 ]
183 assert resp.xpath("//div/content").jmespath("user[*].name").getall() == [
184 "A",
185 "B",
186 "C",
187 "D",
188 ]
189 assert resp.xpath("//div/content").jmespath("total").get() == "4"
190
191 def test_jmestpath_with_re(self) -> None:
192 body = """

Callers

nothing calls this directly

Calls 4

xpathMethod · 0.95
TextResponseClass · 0.90
jmespathMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected