import urllib.request import re import urllib import csv from selenium import webdriver from lxml import etree import requests x=0 header=['日期','开盘价','最高价','最低价','收盘价','涨跌额','涨跌幅','成交量','成交金额','振幅','换手率'] with open('C:\Users\lenovo\Desktop\mmm.csv', 'a+', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(header) for i in range(2017,2019): url="http://quotes.money.163.com/trade/lsjysj_601939.html?year={}&season=".format(i) headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} for k in range(1,5): ur=url+str(k) response=requests.get(ur,headers) html=etree.HTML(response.text) img=html.xpath('//tr[@class=""]/td') ok=html.xpath('//tr[@class="dbrow"]/td') x=x+1 if x==6: break for i in range(len(img)-11,-1,-11): if img[i].text=='2018-02-01'or ok[i-11].text=='2018-02-01': break data=list(img[j].text for j in range(i,i+11)) data2 = list(ok[j-11].text for j in range(i, i + 11)) with open('C:\Users\lenovo\Desktop\mmm.csv', 'a+', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(data) writer.writerow(data2)