Python 在BeautifulSoup中查找标记的内容,但返回空白
我试图解析的xml如下所示:Python 在BeautifulSoup中查找标记的内容,但返回空白,python,beautifulsoup,Python,Beautifulsoup,我试图解析的xml如下所示: 端口在大脑上,某些功能不工作 对不起,我以为我们已经包含了需要的代码。下面是我们一直在测试的内容。在下面的代码中,问题出现在端口1(LeftFrontDriveMotor)上。这是main.cpp中的代码。包括“vex.h”包括“robot config.h”使用名称空间vex 竞争 void leftDrive(){LeftFrontDriveMotor.spin(directionType::fwd,Controller1.Axis3.value(),velo
端口在大脑上,某些功能不工作
对不起,我以为我们已经包含了需要的代码。下面是我们一直在测试的内容。在下面的代码中,问题出现在端口1(LeftFrontDriveMotor)上。这是main.cpp中的代码。包括“vex.h”
包括“robot config.h”使用名称空间vex 竞争 void leftDrive(){
LeftFrontDriveMotor.spin(directionType::fwd,Controller1.Axis3.value(),velocityUnits::pct);
LeftBackDriveMotor.spin(directionType::fwd,Controller1.Axis3.value(),velocityUnits::pct);
void pre\u auton(void){
//初始化机器人配置。不要删除!
vexcodeInit()
]void autonomy(void){void usercontrol(void){
while(true){
Controller1.Axis3.changed(leftDrive);
}int main(){
pre_auton();Competiton.autonomy(autonomy);
Competiton.drivercontrol(usercontrol);while(true){
while(true){
vex::task::sleep>
,以下是robot config.cpp的代码#包括使用名称空间vex的“vex.h”
//用于打印到V5 brain屏幕的brain的全局实例
brain //VEXcode设备
控制器控制器1=控制器(主)
电机左前驱动电机(端口1,比率18_1,错误)
电机左后驱动电机(端口11,比率18_1,错误)
电机右前驱动电机(端口10,比率18_1,正确)
电机右后驱动电机(端口20,比率18_1,正确) /**- 用于初始化使用VEXcode文本中的工具添加的代码/任务/设备。
- 这应该在int main函数开始时调用。
*/ void vexcodeInit(void){
//无需初始化
}]>
https://www.vexforum.com/t/port-on-brain-some-functions-not-working/83135/8
太阳,2020年7月19日16:38:10+0000
www.vexforum.com-post-655101
我需要两个标记之间的文本,但是当我搜索
soup.find('dc:creator')
它只是简单地返回
我认为这可能与文本中的
有关,但我不确定
如何找到带有
BeautifulSoup
的
标记的内容?我使用了html.parser
并得到了结果。出于某种原因,它不适用于lxml
解析器
In [1]: a = """<item>
...: <title>Port on brain, some functions not working</title>
...: <dc:creator>
...: <![CDATA[ @nathankmiles Nathan ]]>
...: </dc:creator>
...: <description>
...: <![CDATA[ <p>Sorry, I thought we had already included the code that would be needed. Here is what we have been
...: using for testing. In the code below, the problem is on Port 1 (LeftFrontDriveMotor).</p> <p>Here is the code
...: from main.cpp.</p> <p><span class="hashtag">#include</span> “vex.h”<br> <span class="hashtag">#include</span>
...: “robot-config.h”</p> <p>using namespace vex;</p> <p>competition Competiton;</p> <p>void leftDrive() {<br> Lef
...: tFrontDriveMotor.spin(directionType::fwd, Controller1.Axis3.value(),velocityUnits::pct);<br> LeftBackDriveMoto
...: r.spin(directionType::fwd, Controller1.Axis3.value(),velocityUnits::pct);<br> }</p> <p>void pre_auton( void )
...: {<br> // Initializing Robot Configuration. DO NOT REMOVE!<br> vexcodeInit();<br> }</p> <p>void autonomous( voi
...: d ) {</p> <p>}</p> <p>void usercontrol( void ) {<br> while(true) {<br> Controller1.Axis3.changed(leftDrive);<b
...: r> }<br> }</p> <p>int main() {<br> pre_auton();</p> <p>Competiton.autonomous( autonomous );<br> Competiton.dri
...: vercontrol( usercontrol );</p> <p>while(true) {<br> vex::task::sleep(100);<br> }<br> }</p> <p>And, here is the
...: code from robot-config.cpp</p> <p><span class="hashtag">#include</span> “vex.h”<br> using namespace vex;</p>
...: <p>// A global instance of brain used for printing to the V5 brain screen<br> brain Brain;</p> <p>//VEXcode De
...: vices<br> controller Controller1 = controller(primary);<br> motor LeftFrontDriveMotor (PORT1, ratio18_1,false)
...: ;<br> motor LeftBackDriveMotor (PORT11, ratio18_1,false);<br> motor RightFrontDriveMotor (PORT10, ratio18_1,tr
...: ue);<br> motor RightBackDriveMotor (PORT20, ratio18_1,true);</p> <p>/**</p> <ul> <li>Used to initialize code/t
...: asks/devices added using tools in VEXcode Text.</li> <li> </li><li>This should be called at the start of your
...: int main function.<br> */</li> </ul> <p>void vexcodeInit(void) {<br> // Nothing to initialize<br> }</p> ]]>
...: </description>
...: <link>https://www.vexforum.com/t/port-on-brain-some-functions-not-working/83135/8</link>
...: <pubDate>Sun, 19 Jul 2020 16:38:10 +0000</pubDate>
...: <guid isPermaLink="false">www.vexforum.com-post-655101</guid>
...: </item>"""
In [2]: from bs4 import BeautifulSoup
In [3]: soup = BeautifulSoup(a, "lxml")
In [4]: soup.find('dc:creator')
Out[4]:
<dc:creator>
</dc:creator>
In [5]: soup = BeautifulSoup(a, "html.parser")
In [6]: soup.find('dc:creator')
Out[6]:
<dc:creator>
<![CDATA[ @nathankmiles Nathan ]]>
</dc:creator>
In [7]: list(soup.find('dc:creator').children)
Out[7]: ['\n', ' @nathankmiles Nathan ', '\n']
In [8]: soup.find('dc:creator').text.strip()
Out[8]: '@nathankmiles Nathan'
[1]中的:a=“”
…:端口在大脑上,某些功能不工作
...:
...:
...:
...:
…:对不起,我以为我们已经包含了所需的代码。这是我们已经包含的代码
…:用于测试。在下面的代码中,问题出在端口1(LeftFrontDriveMotor)上。以下是代码
…:来自main.cpp.#包括“vex.h”
#包括
…:“robot config.h”使用名称空间vex;竞争竞争;无效leftDrive(){
Lef
…:tFrontDriveMotor.spin(directionType::fwd,Controller1.Axis3.value(),velocityUnits::pct);
LeftBackDriveMoto
…:r.spin(directionType::fwd,Controller1.Axis3.value(),velocityUnits::pct);
}void pre_auton(void)
…:{
//初始化机器人配置。不要删除!
vexcodeInit();
}void autonomy(voi
…:d){无效用户控件(void){
而(true){
控制器1.Axis3.changed(leftDrive);}
}int main(){
pre_auton();Competiton.autonomy(autonomy);
Competiton.dri
…:vercontrol(usercontrol);while(true){
vex::task::sleep(100);
}
}下面是
…:来自robot config.cpp的代码#包括使用名称空间vex的“vex.h”
…://用于打印到V5 brain屏幕的brain的全局实例
brain brain;//VEXcode De
…:设备
控制器控制器1=控制器(主);
电机左前驱动电机(端口1,比率18_1,错误)
电机左后驱动电机(端口11,比率18_1,错误);
电机右前驱动电机(端口10,比率18_1,错误);
电机左后驱动电机
…:ue);
电机右后驱动电机(端口20,比率18_1,正确);/**- 用于初始化代码/t
…:使用VEXcode文本中的工具添加的请求/设备。
- 应在开始时调用此命令
…:int主函数。
*/ void vexcodeInit(void){
//无需初始化
}]>
...:
...: https://www.vexforum.com/t/port-on-brain-some-functions-not-working/83135/8
…:太阳,2020年7月19日16:38:10+0000
…:www.vexforum.com-post-655101
...: """
在[2]中:来自bs4导入BeautifulSoup
在[3]中:汤=美汤(a,“lxml”)
[4]中的soup.find('dc:creator')
出[4]:
在[5]中:soup=BeautifulSoup(一个“html.parser”)
[6]中的soup.find('dc:creator')
出[6]:
在[7]中:列表(soup.find('dc:creator').children)
[7]:['\n','@nathankmiles Nathan','\n']
[8]中的soup.find('dc:creator').text.strip()
出[8]:“@nathankmiles Nathan”
如果您没有定义XML名称空间,则XML
解析器将剥离它们。因此,您可以通过
标记进行搜索:
from bs4 import BeautifulSoup
txt = '''<item>
<title>Port on brain, some functions not working</title>
<dc:creator>
<![CDATA[ @nathankmiles Nathan ]]>
</dc:creator>
<description>
<![CDATA[ <p>Sorry, I thought we had already included the code that would be needed. Here is what we have been using for testing. In the code below, the problem is on Port 1 (LeftFrontDriveMotor).</p> <p>Here is the code from main.cpp.</p> <p><span class="hashtag">#include</span> “vex.h”<br> <span class="hashtag">#include</span> “robot-config.h”</p> <p>using namespace vex;</p> <p>competition Competiton;</p> <p>void leftDrive() {<br> LeftFrontDriveMotor.spin(directionType::fwd, Controller1.Axis3.value(),velocityUnits::pct);<br> LeftBackDriveMotor.spin(directionType::fwd, Controller1.Axis3.value(),velocityUnits::pct);<br> }</p> <p>void pre_auton( void ) {<br> // Initializing Robot Configuration. DO NOT REMOVE!<br> vexcodeInit();<br> }</p> <p>void autonomous( void ) {</p> <p>}</p> <p>void usercontrol( void ) {<br> while(true) {<br> Controller1.Axis3.changed(leftDrive);<br> }<br> }</p> <p>int main() {<br> pre_auton();</p> <p>Competiton.autonomous( autonomous );<br> Competiton.drivercontrol( usercontrol );</p> <p>while(true) {<br> vex::task::sleep(100);<br> }<br> }</p> <p>And, here is the code from robot-config.cpp</p> <p><span class="hashtag">#include</span> “vex.h”<br> using namespace vex;</p> <p>// A global instance of brain used for printing to the V5 brain screen<br> brain Brain;</p> <p>//VEXcode Devices<br> controller Controller1 = controller(primary);<br> motor LeftFrontDriveMotor (PORT1, ratio18_1,false);<br> motor LeftBackDriveMotor (PORT11, ratio18_1,false);<br> motor RightFrontDriveMotor (PORT10, ratio18_1,true);<br> motor RightBackDriveMotor (PORT20, ratio18_1,true);</p> <p>/**</p> <ul> <li>Used to initialize code/tasks/devices added using tools in VEXcode Text.</li> <li> </li><li>This should be called at the start of your int main function.<br> */</li> </ul> <p>void vexcodeInit(void) {<br> // Nothing to initialize<br> }</p> ]]>
</description>
<link>https://www.vexforum.com/t/port-on-brain-some-functions-not-working/83135/8</link>
<pubDate>Sun, 19 Jul 2020 16:38:10 +0000</pubDate>
<guid isPermaLink="false">www.vexforum.com-post-655101</guid>
</item>'''
soup = BeautifulSoup(txt, 'xml')
print(soup.find('creator').get_text(strip=True))
或者:您可以使用
html.parser
和bs4.CData
(txt
是问题的html片段):
印刷品:
@nathankmiles Nathan
@nathankmiles Nathan
@nathankmiles Nathan