11.18爬蟲學習(BeautifulSoup類)

果粒就要果粒多發表於2024-11-18

1、把html解析放到txt中

import requests
import os
r=requests.get('http://python123.io/ws/demo.html')
a=r.text
p2='e://guoli/'
if not os.path.exists(p2):
    os.mkdir(p2)
with open(p2+'1.txt','w')as f:
    f.write(a)
    f.close()
from bs4 import BeautifulSoup
b=BeautifulSoup(open(p2+'1.txt'),'html.parser')
print(b.prettify())
with open(p2+'2.txt','w')as f2:
    f2.write(b.prettify())
    f2.close()

相關文章