我有一个列名从 P1 到 P10 的数据框
p1 p2 p3 p4 p5
0 0 0 1 1
0 0 0 0 0
1 1 1 1 1
the total number of 1 has to be displayed at the end of the row
example : first row = 2
second row = 0
third row = 5
Run Code Online (Sandbox Code Playgroud) 我已经实现了以下代码来从维基百科页面提取数据
import bs4
import sys
import requests
res = requests.get('https://en.wikipedia.org/wiki/Agriculture' )
res.raise_for_status()
wiki = bs4.BeautifulSoup(res.text,"html.parser")
for i in wiki.select('p'):
print(i.getText())
Run Code Online (Sandbox Code Playgroud)
该代码根据我的需要从页面中提取所有数据。但是我想使用 Python 将其存储在文本文件中,但我无法做到。该文本文件的名称应为“Agriculture”(如果从 URL 本身中提取该名称则更好,以便可以在多个 wiki 页面上使用)。