应朋友之约,帮他做个爬虫,并且每个网页的数据都分别导入到excel中。 目标网站:http://www./hs_chapter_01.htm 根据我的观察,网页采取的是<td><th>制成表格来存放数据,属于非常简单的类型。因为Python自带有非常好的网页处理模块,因此前后代码花费时间在30分钟。 网站: 
网页源代码: 

需要模块:BeautifulSoup、Request、xlwt 废话不多说,直接上代码: from bs4 import BeautifulSoup from urllib import request url = 'http://www./hs_chapter_'+value0+'.htm' #url='http://www./hs_chapter_01.htm' response = request.urlopen(url) html = html.decode('utf-8') bs = BeautifulSoup(html,'lxml') title = bs.find_all('th') data_list_title.append(data.text.strip()) content = bs.find_all('td') data_list_content.append(data.text.strip()) new_list=[data_list_content[i:i+16] for i in range(0,len(data_list_content),16)] sheet1=book.add_sheet('sheet1',cell_overwrite_ok=True) book.save('sum'+value0+'.xls')
|