我有一个包含刮取值的列表,例如:[<a href="shropshire.html">A Shropshire Lad (David Austin Rose, Austin, 1997) </a>, <a href="agiraud.html">Abbé Giraudier (Hybrid Perpetual, Levet, 1869)</a>, <a href="abelcarr.html">Abel Carrière (Hybrid Perpetual, E. Verdier, 1875)</a>, <a href="abelc.html">Abel Carrière (Illustration from <em>Le Livre d'Or des Roses</em>, 1903)</a>, <a href="darby.html">Abraham Darby® (David Austin Rose, Austin, 1985)</a>, <a href="adammes.html">Adam Messerich (Hybrid Bourbon, Lambert, 1920)<br/> </a>,当我将我的列表转换为 pandas DataFrame 时,只有文本出现在列中,而不是完整的值。如何制作包含每个的全部内容的 df <a> ... </a>,?或者我怎样才能得到一个有两列的 df,一列是 href,另一列是文本?
1 回答

哔哔one
TA贡献1854条经验 获得超8个赞
在这里为其他开发人员发布答案。
您需要从标签中提取 href 和文本
通常像
soup = BeautifulSoup(html.text,'lxml')
with open(filename,'w',newline='',encoding='utf-8') as f:
w = csv.writer(f)
for a in soup.find_all('a',href=True):
text = a.text
link = a['href']
w.writerow([text,link])
然后你可以像这样将这个 CSV 加载到 pandas 中。
pandas.read_csv('filename.csv', columns =['text','url'])
添加回答
举报
0/150
提交
取消