find_parent()方法实现 - BeautifulSoup

时间:2017-04-06 11:28:44

标签: python beautifulsoup

find_parent()方法与find_parents()方法不同,因为它返回第一个直接父级,而不像find_parents()返回整个匹配的父级标记。但为什么这段代码应该是正确的呢?

print(soup.a.string.find_parent("p"))

给出:

   <p>
     <a>"...."</a>
   </p>

p不是字符串的直接父级。

1 个答案:

答案 0 :(得分:1)

URIBuilder uriBuilder = new URIBuilder( GRAPH_FACEBOOK_LEADGEN_CONTEXT_CARDS); uriBuilder.setParameter("access_token", ACCESS_TOKEN); uriBuilder.setParameter("title", "sample-title-1"); uriBuilder.setParameter("style", "PARAGRAPH_STYLE"); uriBuilder.setParameter("content", "['sample-title-1']"); uriBuilder.setParameter("button_text", "Get Started"); HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httppost = new HttpPost(uriBuilder.build()); File file = new File(IMAGE_PATH); ContentBody cbFile = new FileBody(file, ContentType.create(MimeTypeUtils.IMAGE_PNG_VALUE)); byte[] bytes = FileUtils.readFileToByteArray(file); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("cover_photo", cbFile); // builder.addBinaryBody("cover_photo", file); // builder.addBinaryBody("cover_photo", file, // ContentType.create(MimeTypeUtils.IMAGE_PNG_VALUE),IMAGE_PATH); // builder.addBinaryBody("cover_photo", new FileInputStream(file)); // builder.addBinaryBody("cover_photo", bytes, // ContentType.create(MimeTypeUtils.IMAGE_PNG_VALUE),IMAGE_PATH); httppost.setEntity(builder.build()); httppost.addHeader(new BasicHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.getMimeType())); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpClient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } 表示祖先,parents表示找到名为find_parent("p")的第一个祖先

p