'str' object has no attribute 'p' using beautifulsoup

霸气de小男生 提交于 2019-12-25 08:14:41

问题


I have been following a tutorial on using BeautifulSoup, however when I try to read the title or even paragraphs (using soup.p) I get an error saying, "Traceback (most recent call last): File "*****/Tutorial1.py", line 9, in pTag = soup.p AttributeError: 'str' object has no attribute 'p'"

I am still very new to Python, sorry to bother if these is too much of an easy issue but I will greatly appreciate any help. Code given below:

import urllib.request
from bs4 import BeautifulSoup


with urllib.request.urlopen('http://www.bbc.co.uk/sport/0/netball/33717953')    as response:
    page = response.read()
    soup = BeautifulSoup(page, "html5lib")
    soup = soup.prettify()
    pTag = soup.p

    print(pTag)

回答1:


Quoting Beautiful Soup Documentation

The prettify() method will turn a Beautiful Soup parse tree into a nicely formatted Unicode string, with each HTML/XML tag on its own line.

You set an string to soup var here: soup = soup.prettify(). Of course a string has not p property, then crashes.

To find all ps:

...
page = response.read()
soup = BeautifulSoup(page, "html5lib")
for paragraph in soup.find_all('p'):
    do_something_with(paragraph)


来源:https://stackoverflow.com/questions/40844328/str-object-has-no-attribute-p-using-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!