from bs4 import BeautifulSoup
import requests
source = requests.get('http://photographyblogger.net/26-beautiful-horse-pictures/').text
soup = BeautifulSoup(source, features="html.parser")
post = soup.find('article','full')
title = post.h2.text
for summery in post.find_all('p'):
rest = post.find("p", "wp-caption-text");
rest.decompose()
print(summery.text)
what i wanted to achieve is print title , print content and then print caption but currently without using rest i am getting all content and caption together. enter image description here as you can see if i use this code i am getting what i intend but there is some error
似乎您在post.find_all('p')
循环列出了所有'p'
元素,而在循环内,您正在对整个'post'
元素调用find()
,如果"p", "wp-caption-text"
较少"p", "wp-caption-text"
元素然后是'summery'
,那么你的rest
行将是空的,这意味着在空字符串上调用.decompose()
会给出错误'NoneType' object has no attribute 'decompose'
,而你仍然可以看到所有的行你所期望的。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.