def test_main_expected_behavior_with_loop(monkeypatch, requests_mock, capsys): inputs = [-1, MOCK_ALBUM_ID] monkeypatch.setattr("album.validate_input", lambda: inputs.pop(0)) requests_mock.get(URL.format(MOCK_ALBUM_ID), json=MOCK_JSON) album.main() captured = capsys.readouterr() assert captured.out == MOCK_OUTPUT
def test_main_server_unexpected_behavior(monkeypatch, requests_mock, caplog): monkeypatch.setattr("album.validate_input", lambda: MOCK_ALBUM_ID) requests_mock.get(URL.format(MOCK_ALBUM_ID), json=[]) with pytest.raises(RuntimeError, match=SERVER_ERROR_MESSAGE): album.main() assert caplog.record_tuples == [("root", logging.ERROR, SERVER_ERROR_MESSAGE)]
def run(): for os in OS_LIST: page = START_PAGE while 1: # 构造url url = URL.format(os=os, page=page) # get请求 res = requests.get(url) print(f'Fetch: {url}') root = html.fromstring(res.content) rs = root.xpath('//td[@class="useragent"]/a/text()') ua_list.extend(rs) if 50 > len(rs): break page += 1 if page > END_PAGE: break
def make_req(kind, i): return grequests.get(URL.format(kind=kind, episode=i), callback=set_meta({"kind": kind, "i": i}))
def make_req(season, episode, act): return grequests.get( URL.format(season=season, episode=episode, act=act), callback=set_meta({"season": season, "episode": episode, "act": act}))