尝试在 Python 中将已抓取的值列表转换为 Pandas DataFrame。

我有一个包含刮取值的列表,例如:


[<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 时,只有文本出现在列中,而不是完整的值。

http://img2.mukewang.com/644798030001c26c06560406.jpg

如何制作包含每个的全部内容的 df <a> ... </a>,?或者我怎样才能得到一个有两列的 df,一列是 href,另一列是文本?



蝴蝶刀刀
浏览 103回答 1
1回答

哔哔one

在这里为其他开发人员发布答案。您需要从标签中提取 href 和文本通常像soup = BeautifulSoup(html.text,'lxml')with open(filename,'w',newline='',encoding='utf-8') as f:&nbsp; &nbsp; w = csv.writer(f)&nbsp; &nbsp; for a in soup.find_all('a',href=True):&nbsp; &nbsp; &nbsp; &nbsp; text = a.text&nbsp; &nbsp; &nbsp; &nbsp; link = a['href']&nbsp; &nbsp; &nbsp; &nbsp; w.writerow([text,link])然后你可以像这样将这个 CSV 加载到 pandas 中。pandas.read_csv('filename.csv', columns =['text','url'])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python