首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >.get_text()在使用美汤的span上无法正常工作

.get_text()在使用美汤的span上无法正常工作
EN

Stack Overflow用户
提问于 2020-05-12 07:30:06
回答 3查看 55关注 0票数 0

我正在尝试提取following article的主体。这是我使用的代码:

代码语言:javascript
运行
AI代码解释
复制
from bs4 import BeautifulSoup
import requests

a_url = "https://www.business-standard.com/article/current-affairs/up-plans-100-000-covid-beds-as-325-000-stranded-labourers-return-in-2-weeks-120051100865_1.html"
y = requests.get(a_url, headers=headers)
soup2 = BeautifulSoup(y.content, 'html.parser')
body = soup2.find('span',class_= "p-content").get_text()

我认为我应该只得到文本,但这是输出:

代码语言:javascript
运行
AI代码解释
复制
\nspan.p-content div[id^="div-gpt"]{line-height:0;font-size:0}\n\r\n\tAmid the large-scale 
influx of migrant labourers due to the lockdown, the Uttar Pradesh government is planning to make arrangements for 100,000 covid-19 beds across the state.\n\r\n\tAs commercial and 
industrial activity in UP has started reviving under the controlled relaxations announced by the 
government, the state is gearing up to deal with exigencies, with more than a million migrants
 expected to arrive in the near future.document.write("<!--");if(isUserBanner=="free"&&
(displayConBanner==1))document.write("-->");googletag.cmd.push(function()
{googletag.defineOutOfPageSlot(\'/6516239/outofpage_1x1_desktop\',\'div-gpt-ad-1490771277198-
0\').addService(googletag.pubads());googletag.pubads().enableSyncRendering();googletag.enableSer
vices();});\n\ngoogletag.cmd.push(function(){googletag.display(\'div-gpt-ad-1490771277198-
0\');});\n\nvar banHeight=$(".article-middle-banner iframe").height();if(banHeight<=1)
{$(".article-middle-banner").height(0);$(".article-middle-
banner").next().next().remove();}displayConBanner=1;\n\r\n\tIn fact, some 325,000 stranded
 workers have returned in the past two weeks by either train or bus.\n\r\n\tSo far, the state 
government has made arrangements for more than 52,000 covid-19 beds in the public and private 
sectors hospitals.\n\r\n\tChairing a review meeting here, chief minister Yogi Adityanath 
directed officials to ramp up the number of covid beds to 75,000 by May 20 and eventually 
upgrade it to about 100,000 beds in the coming weeks.\nALSO READ: Coronavirus LIVE: 4,213 new 
cases; govt says India recovery rate 31.15%\n\n\r\n\t“The higher number of covid-19 beds would
 ensure that the patients get best medical care in the state whenever required,UP additional
 chief secretary Awanish Kumar Awasthi said this evening.\n\r\n\tThe state has created an 
elaborate network of level 1, 2 and 3 covid-19 hospitals across the state, of which the L1 
pertain to the primary care at the district level, followed by L2 and L3 at the state level 
having superior medical facilities and equipped with oxygen and ventilator support 
respectively.\n\r\n\tBesides, the state has planned to increase the daily testing capacity to 
10,000 per day from less than 5,000 at present. The government is promoting pool testing too so that a larger number of people could be tested in a given period of time.\n\r\n\tAt present, the instance of covid-19 in UP has been the highest in the 21-40 year age category with more than 48 per cent of the total cases in UP, followed by 41-60 year, 0-20 year and 61+ year categories reporting about 26 per cent, 18 per cent and 8 per cent cases respectively.\n\r\n\t“The percentage of men patients in UP is 78.5 per cent compared to women at 21.5 per cent,UP principal secretary, medical and health Amit Mohan Prasad said.\n\r\n\tA majority of the coronavirus patients in UP have been asymptomatic that is the patients do not experience any known symptoms of disease, but are found to be active in sample testing.\n\r\n\tMeanwhile, the state has set the target of arranging for 50-55 trains on a daily basis to speedily evacuate its workers stranded in other states, including Maharashtra, Gujarat, Punjab, Karnataka etc.\n\r\n\t“We are creating a database of migrant workers, who are coming back, so that we could identify their unique skill sets for providing them with suitable employment opportunities in UP itself,” Awasthi informed. The state is looking to provide jobs to more than two million migrant workers.\n

一些额外的HTML和谷歌广告的JS也被检索到。我该如何解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-12 07:41:44

在使用get_text()获取数据之前,需要删除脚本和样式标记中的一些不需要的文本

代码语言:javascript
运行
AI代码解释
复制
from bs4 import BeautifulSoup
import urllib.request

with urllib.request.urlopen(
        "https://www.business-standard.com/article/current-affairs/up-plans-100-000-covid-beds-as-325-000-stranded-labourers-return-in-2-weeks-120051100865_1.html") as response:
    html = response.read()
    soup = BeautifulSoup(html, 'html.parser')
[s.extract() for s in soup('script')]
[s.extract() for s in soup('style')]
body = soup.find('span',class_= "p-content").get_text()
print(body)

你可以用它来获取所有的脚本和样式标签

票数 1
EN

Stack Overflow用户

发布于 2020-05-12 07:39:27

代码语言:javascript
运行
AI代码解释
复制
from bs4 import BeautifulSoup
import urllib.request

# a_url = "https://www.business-standard.com/article/current-affairs/up-plans-100-000-covid-beds-as-325-000-stranded-labourers-return-in-2-weeks-120051100865_1.html"
# y = requests.get(a_url, headers=headers)
# soup2 = BeautifulSoup(y.content, 'html.parser')
# body = soup2.find('span',class_= "p-content").get_text()


def crawl():
    with urllib.request.urlopen("https://www.business-standard.com/article/current-affairs/up-plans-100-000-covid-beds-as-325-000-stranded-labourers-return-in-2-weeks-120051100865_1.html") as response:
        html = response.read()
        soup = BeautifulSoup(html, 'html.parser')

    for b in soup.find_all('span',{'class':'p-content'}):
        print(b.text)

crawl()

我想你得到了你想要的东西。

我想描述一下代码的细节。但是我很难写英语。所以我希望了解你自己!

票数 0
EN

Stack Overflow用户

发布于 2020-05-12 07:48:44

这里有一个解决方案。

代码语言:javascript
运行
AI代码解释
复制
from bs4 import BeautifulSoup
import requests

a_url = "https://www.business-standard.com/article/current-affairs/up-plans-100-000-covid-beds-as-325-000-stranded-labourers-return-in-2-weeks-120051100865_1.html"
y = requests.get(a_url)
soup2 = BeautifulSoup(y.content, 'html.parser')
body = soup2.select('span.p-content p')
for item in body:
    print(item.getText())

不要让Yogi更改你的名字,LOL(IYKWIM)。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61746078

复制
相关文章
Apache编译后无法正常工作
因为某个场景的需求,要在一个国产系统Rocky4.2(国产凝思4.2操作系统)上安装Apache,虽说此系统是基于Redhat 5.8开发的,但是发现yum安装源包管理,RPM命令倒是能用,但是底层依赖完全没有,这就尴尬了,so,只能源码编译安装了。
后场技术
2020/09/03
2.9K0
vue在IE下无法正常工作,Promise未定义?
用vue写了一个日历组件,在Firefox、Edge、Chrome以及360等浏览器极速模式中运行一切正常,如图:
Yiiven
2022/12/15
4.3K0
vue在IE下无法正常工作,Promise未定义?
小程序<live-pusher>、<live-player>标签无法正常使用?
出于政策和合规的考虑,微信暂时没有放开所有小程序对 <live-pusher> 和 <live-player> 标签的支持:
hhualiu
2019/03/11
2.3K0
小程序<live-pusher>、<live-player>标签无法正常使用?
Android Span在项目中的几种使用方式
1.字符串中 改变字体颜色和大小 2.字符串中 改变文字颜色 并 添加点击事件 3.字符串中 增加外框和改变文字颜色及字体大小 1.改变文字颜色 ForegroundColorSpan /\*\* \* 自定义颜色 \* \* @param content 全部文本 \* @param keyWord 需要变色的关键字 \* @param color 颜色 \*/ public static SpannableStringBuilder getSpan(String conte
Jingbin
2019/03/21
1.2K0
NPAPI 插件【Silverlight】无法在 Chrome 42 版及更高版本上正常运行
您可以利用插件在浏览器中添加一些额外的功能。例如,您可以观看某些类型的视频或者玩网页版游戏。
杨强生
2019/03/05
2.8K0
pip无法正常使用解决办法
python -m pip install xxx 相当于import,叫做当做模块来启动
一点儿也不潇洒
2018/08/07
1.1K0
K3组件kdsvrmgr无法正常工作
1.在服务器上创建一个用户名和口令,和客户端登陆的用户名和口令一致的;  2.通过网上邻居访问服务器,如果没有提示输入用户和密码,那么网络没问题;  3.看COM+和DTC服务有没有设置好;  4.是XP sp3系统或Home系统的话,可能不稳定;  5.再者看感染病毒没有。 。
py3study
2020/01/08
4.7K0
Android Span在项目中的几种使用方式
1.字符串中 改变字体颜色和大小 2.字符串中 改变文字颜色 并 添加点击事件 3.字符串中 增加外框和改变文字颜色及字体大小 1.改变文字颜色 ForegroundColorSpan /** * 自定义颜色 * * @param content 全部文本 * @param keyWord 需要变色的关键字 * @param color 颜色 */ public static SpannableStringBuilder getSpan(String content, String k
Jingbin
2019/03/22
7050
python 升级导致yum无法正常使用
    昨天安装django的时候有些命令用不了,一查应该是python版本过低,目前版本2.6.6,于是编译安装了2.7.2版本,编译完成后做了个软连接加到path路径里面,使python调用的2.7版本。但是问题来了,当使用yum的时候报错使用不了,找不到模块。
py3study
2020/01/15
4.1K1
FastAPI自动生成的文档无法展开、正常使用
这两天我的LanAPI那个项目没怎么关心,昨天去看文档的时候发现加载不出了。 刚好今天在FastAPI交流群看到了解决方案 demo.zip大小:322.2K 已经过安全软件检测无毒,请您放心下载。
SingYi
2022/07/14
9170
FastAPI自动生成的文档无法展开、正常使用
Tcplayer 在ios无法正常播放直播流
var player = new TcPlayer('id_test_video', {
用户6942005
2020/12/21
1.9K9
springboot 使用 freemarker 无法正常跳转的问题?
参考:https://blog.csdn.net/Lin_xiaofeng/article/details/79122053
别先生
2019/07/30
1.5K0
springboot 使用 freemarker 无法正常跳转的问题?
测评 PS 最新 AI 功能 在美宣上的使用
尽管目前只是测试阶段,但无论是操作的流畅性,还是生成效果上,Generative Fill都展示出了强大的潜力和影响力。
腾讯大讲堂
2023/08/05
5460
测评 PS 最新 AI 功能 在美宣上的使用
解决 macOS Ventura 使用 ssh/git 等无法正常使用的问题
关键词:macOS Ventura、Ventura、SSH、git、Permission denied
他叫自己MR.张
2022/11/02
3.8K0
正常的工作流程
修改文件,将它们更新的内容添加到索引中。 $ git add file1 file2 file3 你现在为commit做好了准备,你可以使用git diff命令再加上–cached参数,看看哪些文件将被提交(commit)。 (如果没有–cached参数,git diff会显示当前你所有已做的但没有加入到索引里的修改。)你也可以使用git status命令来获得当前项目的一个状况。
用户3004328
2018/09/06
7900
RazorEngine 3.3 在Mono 3.2上正常运行
RazorEngine 是一个简化的模板引擎基于微软新的Razor 解析引擎, Razor是在 ASP.NET MVC3 和 Web Pages中引入的。RazorEngine 提供了一个外包装和额外
张善友
2018/01/29
6250
解决 macOS Ventura 使用 ssh、git 等无法正常使用的问题
关键词:macOS Ventura、Ventura、SSH、git、Permission denied
他叫自己MR.张
2023/05/24
6670
使用TamperMonkey解决Google被墙stackoverflow无法正常使用的问题
Stackoverflow是广大程序猿赖以生存的工具之一,在stackoverflow搜索技术问题得到答案的质量和正确率远远高于其他平台。但是这么一个好网站居然因为Google被墙而无法正常使用(无法登录,无法评论、回答问题等)着实让人不爽呀。以前还有VPN可以用的,现在大部分VPN都被查封了。。程序员的日子真的是越来越难过了呀。。所以我下决心想办法自己解决这个令人头疼问题。
MudOnTire
2019/05/26
2.6K0
点击加载更多

相似问题

Python美汤没有定位span元素

20

如何在美汤中使用get_text()时更改unicode格式

20

用美汤从span中提取元素

24

美汤的使用

16

如何使用美汤获取html中span标签的值?

12
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档