我正在尝试废弃一个XML文件,并从XML文件上的标记创建一个数据帧。我用pyspark开发数据块。
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<shorttitle>shorttitle_1</shorttitle>
<shorttitle>shorttitle_2</shorttitle>
<shorttitle>shorttitle_3</shorttitle>
<shorttitle>shorttitle_4</shorttitle>
</note>
我的代码似乎要从页面中删除XML并从标记创建一个列表,但是当我创建数据帧并尝试输入所述列表时,我只看到一个dataframe包含空值。
代码:
from pyspark.sql.types import *
from pyspark.sql.functions import *
import requests
from bs4 import BeautifulSoup
res = requests.get("http://files.fakeaddress.com/files01.xml")
soup = BeautifulSoup(res.content,'html.parser')
short_title = soup.find_all('shorttitle')[0:2]
field = [StructField("Short_Title",StringType(), True)]
schema = StructType(field)
df = spark.createDataFrame(short_title, schema)
输出:
+-----------+
|Short_Title|
+-----------+
| null|
| null|
+-----------+
想要的输出:
+-------------+
|Short_Title |
+-------------+
|shorttitle_1 |
|shorttitle_2 |
+-------------+