openpyxl保存EXCEL,程序保存不了数据进入EXCEL?
openpyxl保存EXCEL,程序保存不了数据进入EXCEL?
from bs4 import BeautifulSoup
import requests
from openpyxl import Workbook
from openpyxl import load_workbook
class GetUrl(object):
def __init__(self,url):
self.url = url
self.urls=self.getLinks(self.url)
self.ites=self.getInfo(self.urls)
self.spider(self.ites)
def getLinks(self,url):
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
links = soup.select('#page_list > ul > li > a')
for link in links:
href = link.get("href")
return href
def judgment_sex(self,class_name):
if class_name == ['member_ico1']:
return '女'
else:
return '男'
def getInfo(self,url):
ites=[]
its=[]
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
tittles = soup.select('div.pho_info > h4')
addresses = soup.select('span.pr5')
prices = soup.select('#pricePart > div.day_l > span')
imgs = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img')
names = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a')
sexs = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')
for tittle, address, price, img, name, sex in zip(tittles,addresses,prices,imgs,names,sexs):#遍历
its = [tittle.get_text().strip(),address.get_text().strip(),price.get_text(),img.get("src"),name.get_text(),self.judgment_sex(sex.get("class"))]
for ites in its:
return ites
def spider(self, ites):
dest_filename='wsf3.xlsx'
wb=load_workbook(dest_filename)
reward_ws_hearder = ['tittle','address','price','img','name','sex']
reward_ws = wb.create_sheet(title='分析')
reward_ws.append(reward_ws_hearder)
start_list = 0
list_step = 6
end_list = start_list + list_step
for i in range(1,4):#根据行数去循环次数
reward_ws.append(self.getInfo(ites[start_list:end_list]))#第一次循环0-6个数,第二次循环6-12个数,-----;
start_list += list_step
end_list = start_list + list_step
wb.save("C:/Users/Administrator/Desktop/1x1.xlsx")
if name == '__main__':
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}
urls = ['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(number) for number in range(1,3)]
for single_url in urls:
url= GetUrl(single_url)
time.sleep(2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是load了一个文件,修改后保存到了另一个文件
=========
save被你注释了