在 python 中使用 beautifullsoup4 抓取网页时出现奇怪的文本缩进

     2023-02-15     53

关键词:

【中文标题】在 python 中使用 beautifullsoup4 抓取网页时出现奇怪的文本缩进【英文标题】:weird text indentation when web scraping with beautifullsoup4 in python 【发布时间】:2021-12-31 21:56:29 【问题描述】:

我正在尝试网页抓取 github


这是代码:

import requests as req
from bs4 import BeautifulSoup

urls = [
  "https://github.com/moom825/Discord-RAT",
  "https://github.com/freyacodes/Lavalink",
  "https://github.com/KagChi/lavalink-railways",
  "https://github.com/KagChi/lavalink-repl",
  "https://github.com/Devoxin/Lavalink.py",
  "https://github.com/karyeet/heroku-lavalink"]



r = req.get(urls[0])

soup = BeautifulSoup(r.content,"lxml")

title = str(soup.find("p",attrs="class":"f4 mt-3").text)
print(title)

当我运行程序时,我没有收到任何错误,但缩进很奇怪

请任何人帮我解决这个问题 我正在使用replit

【问题讨论】:

【参考方案1】:

Github 有 a really good API

您可以在.text 之后使用.strip(),然后它将删除空格。

import requests as req
from bs4 import BeautifulSoup

urls = [
  "https://github.com/moom825/Discord-RAT",
  "https://github.com/freyacodes/Lavalink",
  "https://github.com/KagChi/lavalink-railways",
  "https://github.com/KagChi/lavalink-repl",
  "https://github.com/Devoxin/Lavalink.py",
  "https://github.com/karyeet/heroku-lavalink"]



r = req.get(urls[0])

soup = BeautifulSoup(r.content,"lxml")

title = str(soup.find("p",attrs="class":"f4 mt-3").text.strip())
print(title)

【讨论】:

好吧,我是愚蠢的,我用 python 编程了 1.5 年,我不知道这个!! 你总是瘦一些——我也是 我猜这就是progging的工作原理? 等一下,我不能接受这个答案 我们需要 8 分钟才能接受

如何在 python 脚本中使用 awscli?

】如何在python脚本中使用awscli?【英文标题】:Howtouseawscliinsidepythonscript?【发布时间】:2014-07-0919:34:22【问题描述】:我正在使用带有awscli的awsec2服务。现在我想将我在控制台中输入的所有命令放入一个python脚本中。我看到如果... 查看详情

在 Snowflake 中使用 Python 创建文件格式

】在Snowflake中使用Python创建文件格式【英文标题】:FileformatcreationusingPythoninSnowflake【发布时间】:2020-03-0318:18:54【问题描述】:我们使用python进行数据加载,所以我们需要使用python在雪花中创建文件格式。我尝试通过python创建... 查看详情

使用 Python 在 Windows 中构建 postgreSQL

】使用Python在Windows中构建postgreSQL【英文标题】:BuildpostgreSQLinWindowswithPython【发布时间】:2014-06-0620:54:37【问题描述】:我可以在Windows中使用Python从源代码安装和构建postgreSQL吗?坚固吗?目前在他们的文档中,他们有VisualC++asth... 查看详情

在 python 中使用 soffice,Command 在终端中有效,但在 Python 子进程中无效

】在python中使用soffice,Command在终端中有效,但在Python子进程中无效【英文标题】:Usingsofficewithinpython,CommandworksinterminalbutnotinPythonsubprocess【发布时间】:2016-06-1208:38:04【问题描述】:我在Python中遇到了最令人沮丧的libreoffice问题... 查看详情

使用 Python 在 Google 中搜索

】使用Python在Google中搜索【英文标题】:SearchinginGooglewithPython【发布时间】:2016-12-0218:57:25【问题描述】:我想使用python脚本在Google中搜索文本并返回每个结果的名称、描述和URL。我目前正在使用此代码:fromgoogleimportsearchip=raw_i... 查看详情

在 Python 中使用 Weka 生成的决策树

】在Python中使用Weka生成的决策树【英文标题】:UsingWekageneratedDecisionTreeinPython【发布时间】:2021-04-3006:10:12【问题描述】:我使用Weka-GUI生成了一个决策树,并尝试在我的Python代码中使用生成的决策树。Python中是否有加载DT模型... 查看详情

在 javascript 中使用 python 输出

】在javascript中使用python输出【英文标题】:Usingapythonoutputinjavascript【发布时间】:2021-04-1306:22:06【问题描述】:我们想将一个布尔值从python发送到javascript,以便我们可以在我们的html网站中使用它。我们尝试使用套接字,但这对... 查看详情

在 Python 中使用 @ [重复]

】在Python中使用@[重复]【英文标题】:Useof@inPython[duplicate]【发布时间】:2020-08-1616:54:26【问题描述】:我在IDLE中使用help()函数看到了python的内置super()方法的方法和文档。我遇到了这段代码Thisworksforclassmethodstoo:|classC(B):|@classmetho... 查看详情

如何在 Python 中使用 ITK 类

】如何在Python中使用ITK类【英文标题】:HowdoIuseanITKclassinPython【发布时间】:2015-05-1515:00:25【问题描述】:我在CPP中使用ITK编写了一个类,它读取目录中的所有文件,然后对它们进行平均。我想在使用Python构建的管道中使用这个... 查看详情

python在python中使用全局变量(代码片段)

查看详情

如何在 Python 中使用 Selenium 在 Firefox 中禁用 Flash?

】如何在Python中使用Selenium在Firefox中禁用Flash?【英文标题】:HowtodisableFlashinFirefoxwithSeleniuminPython?【发布时间】:2013-04-3016:17:55【问题描述】:尝试使用配置文件设置在Python中使用Selenium在Firefox中禁用Flash。Thisquestion指定了一种... 查看详情

如何在python中使用nice命令?

】如何在python中使用nice命令?【英文标题】:Howtousenicecommandinpython?【发布时间】:2013-04-1905:48:30【问题描述】:如何在python中使用nice?我有一个简单的bash脚本:nice-n9cp/var/tmp/1/var/tmp/2什么是python替代品?【问题讨论】:【参考... 查看详情

如何在 Spark 中使用 Python 查找 DataFrame 中的分区数以及如何在 Spark 中使用 Python 在 DataFrame 中创建分区

】如何在Spark中使用Python查找DataFrame中的分区数以及如何在Spark中使用Python在DataFrame中创建分区【英文标题】:HowtofindnumberofpartitionsinaDataFrameusingPythoninsparkAndhowtocreatePartitionsinaDataFramewithPythoninspark【发布时间】:2021-08-2013:09:09【问... 查看详情

NameError 在 python 中使用 execfile

】NameError在python中使用execfile【英文标题】:NameErrorusingexecfileinpython【发布时间】:2010-10-2916:53:03【问题描述】:我的应用程序有一个使用execfile动态执行python脚本的按钮。如果我在脚本中定义一个函数(例如spam())并尝试在另... 查看详情

在 python 中使用命令行时出现语法错误

】在python中使用命令行时出现语法错误【英文标题】:syntaxerrorwhenusingcommandlineinpython【发布时间】:2021-11-1400:26:33【问题描述】:我是python的初学者,目前无法使用命令行。我有一个脚本test.py(仅包含print("Hello.")),它... 查看详情

为啥我应该在 python 中使用类方法? [复制]

】为啥我应该在python中使用类方法?[复制]【英文标题】:WhyshouldIuseaclassmethodinpython?[duplicate]为什么我应该在python中使用类方法?[复制]【发布时间】:2017-09-1205:21:45【问题描述】:我正在python的某个类中编写一个函数,人们建... 查看详情

如何使用 Python 在图像中隐藏文件?

】如何使用Python在图像中隐藏文件?【英文标题】:HowdoIhideafileinsideanimagewithPython?【发布时间】:2013-06-2015:10:15【问题描述】:我知道可以在批处理中使用带有“/B”开关的“复制”命令,即:copy/Bimagefile+hiddenfilenewfile我的问题... 查看详情

在 Python 中使用的高级邮件

】在Python中使用的高级邮件【英文标题】:AdvancedmailtouseinPython【发布时间】:2017-01-0902:21:14【问题描述】:我正在尝试使用Python中的自定义收件人、主题和正文发送一封电子邮件。这意味着我不想使用Python\'ssmtppackages,而是使... 查看详情