openpyxl保存EXCEL,程序保存不了数据进入EXCEL?

发布于 2022-09-06 15:52:42 字数 2643 浏览 15 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2022-09-13 15:52:42

你是load了一个文件,修改后保存到了另一个文件

=========
save被你注释了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文