【问题标题】:how do I extract text from a webpage when the specific text is in a span tag当特定文本位于跨度标签中时,如何从网页中提取文本
【发布时间】:2021-02-02 23:51:56
【问题描述】:

我正在尝试编写一个 python 程序,该程序通过谷歌课堂页面运行并提取我老师写的任何新帖子的文本。我尝试运行许多不同的建议代码,但没有一个对我有用。每篇文章的 CSS 都在以下标签下:“此处的任何文本”。我所做的任何提取文本的尝试都失败了。我的最终目标是编写一个程序,该程序将获取任何这些标签下的所有文本并过滤它们以仅返回包含特定关键字的帖子。主要问题是当我尝试提取文本时,程序总是返回一个空 array/[] 尽管那里有文本。任何帮助表示赞赏。

【问题讨论】:

  • 您应该为您的问题提供更多背景信息,因为如果不了解 CSS 的外观、您迄今为止尝试过的内容等,很难为您提供帮助。这里有更多关于如何编写关于堆栈溢出的好问题:stackoverflow.com/help/how-to-ask

标签: python html web-scraping


【解决方案1】:

这是一个例子: 使用stackoverflow.com,我们可以在两个span标签之间选择标题。

import urllib.request
import re

fid=urllib.request.urlopen('http://stackoverflow.com/')
webpage=fid.read().decode('utf-8')
print(webpage + "\n") # print the contents of the webpage
title = re.findall("<title>(.*?)</title>", webpage) #copy the title in between the two span tags
print("The Title of the webpage is:\n")
print(title)

Example

import re
spantag = '<p><span style = "color:#8866ff;"> Hello World </span>'
all = re.findall("\;\"\>(.*?)</span>", spantag)
print(all)

Example2

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2011-11-19
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多