MCPcopy
hub / github.com/scrapy/scrapy / test_json_has_html

Method test_json_has_html

tests/test_selector.py:109–144  ·  view source on GitHub ↗

Sometimes the information is returned in a json wrapper

(self)

Source from the content-addressed store, hash-verified

107@pytest.mark.skipif(not PARSEL_18_PLUS, reason="parsel < 1.8 doesn't support jmespath")
108class TestJMESPath:
109 def test_json_has_html(self) -> None:
110 """Sometimes the information is returned in a json wrapper"""
111
112 body = """
113 {
114 "content": [
115 {
116 "name": "A",
117 "value": "a"
118 },
119 {
120 "name": {
121 "age": 18
122 },
123 "value": "b"
124 },
125 {
126 "name": "C",
127 "value": "c"
128 },
129 {
130 "name": "<a>D</a>",
131 "value": "<div>d</div>"
132 }
133 ],
134 "html": "<div><a>a<br>b</a>c</div><div><a>d</a>e<b>f</b></div>"
135 }
136 """
137 resp = TextResponse(url="http://example.com", body=body, encoding="utf-8")
138 assert (
139 resp.jmespath("html").get()
140 == "<div><a>a<br>b</a>c</div><div><a>d</a>e<b>f</b></div>"
141 )
142 assert resp.jmespath("html").xpath("//div/a/text()").getall() == ["a", "b", "d"]
143 assert resp.jmespath("html").css("div > b").getall() == ["<b>f</b>"]
144 assert resp.jmespath("content").jmespath("name.age").get() == "18"
145
146 def test_html_has_json(self) -> None:
147 body = """

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected