pub struct Response {
pub url: Url,
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
pub request_url: Url,
pub meta: DashMap<Cow<'static, str>, Value>,
}Expand description
Represents an HTTP response received from a server.
Fields§
§url: UrlThe final URL of the response after any redirects.
status: StatusCodeThe HTTP status code of the response.
headers: HeaderMapThe headers of the response.
body: BytesThe body of the response.
request_url: UrlThe original URL of the request that led to this response.
meta: DashMap<Cow<'static, str>, Value>Metadata associated with the response, carried over from the request.
Implementations§
Source§impl Response
impl Response
Sourcepub fn request_from_response(&self) -> Request
pub fn request_from_response(&self) -> Request
Reconstructs the original Request that led to this response.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>
Deserializes the response body as JSON.
Sourcepub fn to_html(&self) -> Result<Html, Utf8Error>
pub fn to_html(&self) -> Result<Html, Utf8Error>
Parses the response body as HTML.
Examples found in repository?
examples/quotes_scraper.rs (line 38)
37 async fn parse(&mut self, response: Response) -> Result<ParseOutput<Self::Item>, SpiderError> {
38 let html = response.to_html()?;
39 let mut output = ParseOutput::new();
40
41 for quote_element in html.select(&".quote".to_selector()?) {
42 let text = quote_element
43 .select(&".text".to_selector()?)
44 .next()
45 .map(|e| e.text().collect::<String>().trim().to_string())
46 .unwrap_or_default();
47
48 let author = quote_element
49 .select(&".author".to_selector()?)
50 .next()
51 .map(|e| e.text().collect::<String>().trim().to_string())
52 .unwrap_or_default();
53
54 let tags: Vec<String> = quote_element
55 .select(&".tags .tag".to_selector()?)
56 .map(|e| e.text().collect::<String>().trim().to_string())
57 .collect();
58 let tags_str = tags.join(", ");
59
60 let item = QuoteItem {
61 text,
62 author,
63 tags: tags_str,
64 };
65 output.add_item(item);
66 }
67
68 if let Some(next_href) = html
69 .select(&".next > a".to_selector()?)
70 .next()
71 .and_then(|a| a.attr("href"))
72 {
73 let next_url = response.url.join(next_href)?;
74 let next_request = Request::new(next_url);
75 output.add_request(next_request);
76 }
77
78 Ok(output)
79 }Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Response
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more