1. import requests
    2. import csv
    3. import re
    4. url = 'http://www.weather.com.cn/weather/101070301.shtml'
    5. headers = {
    6. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
    7. }
    8. response = requests.get(url, headers=headers)
    9. response.encoding = 'utf-8'
    10. html = response.text
    11. result = re.match(r'.*(<ul class="t clearfix">.*?</ul>).*', html, re.S) # 匹配多行数据
    12. ul = result.group(1)
    13. # print(ul)
    14. lis = re.findall(r'<li.*?>.*?</li>', ul, re.S)
    15. print(lis)
    16. pattern = re.compile(r'<li.*?>.*?<h1>(.*?)</h1>.*?<p.*?>(.*?)</p>.*?<span>(.*?)</span>.*?<i>(.*?)</i>.*?<i>(.*?)</i>.*?</li>', re.S)
    17. lst = []
    18. for i in lis:
    19. r = pattern.match(i)
    20. tu = (r.group(1), r.group(2), r.group(3), r.group(4), r.group(5))
    21. lst.append(tu)
    22. # print(lst)
    23. with open('weather.csv', 'w', encoding='utf-8-sig', newline='') as f:
    24. writer = csv.writer(f)
    25. writer.writerow(['日期', '天气', '最高气温', '最低气温', '风力'])
    26. writer.writerows(lst)