Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在beautifulsoup4中忽略子元素_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 如何在beautifulsoup4中忽略子元素

Python 如何在beautifulsoup4中忽略子元素,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在使用BeautifulSoup4从一个网站上提取有关课程设置的数据 我正试图从元素中提取课程描述 当我跑步时: course_descriptions = soup.findAll("p") 我得到: <p><b>INFO 101 Social Networking Technologies (5) I&amp;S/NW</b><br/>Explores today's most popular social networks,

我正在使用BeautifulSoup4从一个网站上提取有关课程设置的数据

我正试图从
元素中提取课程描述

当我跑步时:

course_descriptions = soup.findAll("p")
我得到:

<p><b>INFO 101 Social Networking Technologies (5) I&amp;S/NW</b><br/>Explores today's most 
popular social networks, gaming applications, and messaging applications. Examines 
technologies, social implications, and information structure. Focuses on logic, databases, 
networked delivery, identity, access, privacy, ecommerce, organization, and retrieval.
<br/><a href="https://uwstudent.washington.edu/course/#/courses/INFO101" target="_blank">
View course details in MyPlan: INFO 101</a></p>,
<p><b>INFO 102 Gender and Information Technology (5) I&amp;S, DIV</b><br/>Explores the social 
construction of gender in relation to the history and contemporary development of 
information technologies. Considers the importance of diversity and difference in the 
design and construction of innovative information technology solutions. Challenges 
prevailing viewpoints about who can and does work in the information technology field. 
Offered: A.<br/><a href="https://uwstudent.washington.edu/course/#/courses/INFO102" 
target="_blank">View course details in MyPlan: INFO 102</a></p>,
信息101社交网络技术(5)I&;S/NW
探索当今最流行的 流行的社交网络、游戏应用程序和消息传递应用程序。检查 技术、社会影响和信息结构。关注逻辑、数据库、, 网络交付、身份、访问、隐私、电子商务、组织和检索。

, 信息102性别与信息技术(5)I&;S、 DIV
探索社会 性别建构与中国历史和当代发展 信息技术。考虑多样性和差异的重要性 设计和构建创新的信息技术解决方案。挑战 关于谁能够并确实在信息技术领域工作的主流观点。 报价:A.


我想得到这些结果,但是没有
标记中的内容。如何从结果中排除它们?

获取课程描述后,您可以迭代p标记,然后使用删除标记


列表文本将仅包含p标记内的内容。希望有帮助。

我得到了
AttributeError:'NoneType'对象没有属性'decompose'
检查编辑,一些p标记可能不包含b标记,这就是它引发异常的原因。
text = list()
for item in course_descriptions:
    # some p tags could not have b tags at all.
    try:
        item.b.decompose()
    except:
        pass
    text.append(item.text)