2 回答
TA贡献1876条经验 获得超7个赞
尝试这个:
from bs4 import BeautifulSoup
import urllib.request # web access
import re
url = "https://wsc.nmbe.ch/family/87/Senoculidae"
page = urllib.request.urlopen(url) # conntect to website
try:
page = urllib.request.urlopen(url)
except:
print("Ups!")
soup = BeautifulSoup(page, 'html.parser')
#div = soup.find(text=True, recursive=)
regex = re.compile('^speciesTitle')
content_lis = soup.find_all('div', attrs={'class': regex})
file = ''
for cl in content_lis:
a = cl.select_one('div a strong i')
b = cl.find(text=True, recursive=False)
c = cl.select_one('span')
cc = re.findall("[\w]+", c.text)[0]
file += f'{a.get_text(strip=True)};{b.strip()};{cc}\n'
with open('file.csv', 'w') as f:
f.write(file)
保存一个文件:
Senoculus albidus;(F. O. Pickard-Cambridge, 1897);Brazil
Senoculus barroanus;Chickering, 1941;Panama
Senoculus bucolicus;Chickering, 1941;Panama
Senoculus cambridgei;Mello-Leitão, 1927;Brazil
Senoculus canaliculatus;F. O. Pickard-Cambridge, 1902;Mexico
Senoculus carminatus;Mello-Leitão, 1927;Brazil
Senoculus darwini;(Holmberg, 1883);Argentina
Senoculus fimbriatus;Mello-Leitão, 1927;Brazil
Senoculus gracilis;(Keyserling, 1879);Guyana
Senoculus guianensis;Caporiacco, 1947;j
Senoculus iricolor;(Simon, 1880);Brazil
Senoculus maronicus;Taczanowski, 1872;French
等等...
TA贡献1831条经验 获得超10个赞
此代码基于您的示例数据集:
lst=[
'Senoculus albidus#(F. O. Pickard-Cambridge, 1897)#|#| Brazil',
'Senoculus barroanus#Chickering, 1941#|#| Panama',
'Senoculus bucolicus#Chickering, 1941#|#| Panama'
]
lst2 = [s.replace('|',"").split('#') for s in lst]
lst3=[]
for s in lst2:
lst3.append(';'.join([sx.strip() for sx in s]).replace(';;',';'))
for s in lst3:
print(s)
输出
Senoculus albidus;(F. O. Pickard-Cambridge, 1897);Brazil
Senoculus barroanus;Chickering, 1941;Panama
Senoculus bucolicus;Chickering, 1941;Panama
--- 根据请求者评论更新 ---
在最后一个循环中添加一行:
for li in content_lis:
con = li.get_text("#",strip=True).split("\n")[0]
con = ';'.join(sx.strip() for sx in con.replace('|',"").split('#')).replace(';;',';') # add this line
print(con)
添加回答
举报