源代码如下,欢迎交流! url9 = "http://datachart.500.com/dlt/history/newinc/history.php?start=23101&end=23135" resp = requests.get(url9) hm = etree.HTML(resp.text) # 在返回页面内容的任意位置查找id=tdata的tbody标签,并取其下所有的tr标签内容,赋给trs列表 trs = hm.xpath("//tbody[@id='tdata']/tr") f = open('cs_data.txt' , 'w') # 将攫取的数据存到data.csv文件 # filename = 'F://dlt1//fx_data.txt' current_path = os.getcwd() filename = '{}\\cs_data.csv'.format(current_path) fo = io.open(filename , 'w' , encoding='utf-8') fo.truncate() for tr in trs: data_lst = tr.xpath('td/text()') # 准备写入文件,以备后用,csv文件是pandas能直接读取的,最有效率 # 去掉列表中的逗号和\xa0,使用的是列表