用了bs4完成了
Topic sourceBeautiful soup 很强大 我copy了你的代码
import os
from urllib import request
from bs4 import BeautifulSoup
'html_code.txt contains the source code of url'
def fetch_xml(url):
# req = request.Request(url)
# page = request.urlopen(req)
# f = page.read()
file_path = os.path.join(os.path.dirname(__file__), 'html_code.txt')
f = open(file_path, 'r').read()
soup = BeautifulSoup(f, 'html.parser')
for j in soup.findAll('div', {'class': 'BdB Bds(d) Bdbc(#fff.12) Fz(1.2em) Py(2px) O(0) Pos(r) forecast-item'}):
print('星期:', j.find('div', class_="D(ib) Va(m) W(1/4)").text)
print('天气:', j.select('span[class="D(ib) Va(m) W(1/4) Ta(c)"] > img')[0]['title'].strip())
a = j.select('span[class="D(ib) Va(m) W(1/4) Ta(end)"]')[0].get_text()
print('温度:', 'high:%sF, low:%sF' % (a[0:3], a[3:6]))
print('-' * 30)
if __name__ == "__main__":
fetch_xml()
Thanks
- 1
水滴竹沙