方知有 2019-07-15 15:12 采纳率: 0%
浏览 848

爬虫部分数据写入excel失败

最近学习爬虫,参考大佬代码,但是爬取结果没有抬头,第三个爬取的数据并没有插入excel中

import requests
from lxml import etree
from openpyxl import Workbook
import random

class tengxun():
    def __int__(self):
        self.url = 'https://ke.qq.com/course/list?mt=1001&page={}'
        self.header = {
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
            "Connection": "keep - alive",
        }
        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.append(['title', 'link', 'now_reader'])

    def geturl(self):
        self.url = 'https://ke.qq.com/course/list?mt=1001&page={}'
        url = [self.url.format(i) for i in range(1,5)]
        return url

    def prase_url(self,url):
        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
            "Connection": "keep - alive",
        }
        response = requests.get(url, headers=self.header, timeout=5)
        return response.content.decode('gbk', 'ignore')

    def get_list(self,html_str):
        html = etree.HTML(html_str)
        connect_list = []
        lists = html.xpath("//li[@class ='course-card-item']")
        for list in lists:
             item = {}
             item['title'] = ''.join(list.xpath("./h4/a[@class = 'item-tt-link']/text()"))
             item['link'] = ''.join(list.xpath("./a[@class = 'item-img-link']/@href"))
             item['now_reader'] = ''.join(list.xpath("./div[@class = 'item-line item-line--moddle']/span[@class='line-cell item-user']/text()"))
             connect_list.append(item)
        return connect_list

    def save_list(self, connects):
        self.wb = Workbook()

        self.ws = self.wb.active

        for connect in connects:
            self.ws.append([connect['title'], connect['link'], connect['now_reader']])
        print('保存成功页招聘信息')

    def run(self):
        url_list = self.geturl()
        for url in url_list:
            html_url = self.prase_url(url)
            connects = self.get_list(html_url)
            self.save_list(connects)
        self.wb.save(r'C:\Users\Administrator\Desktop\resource\UA_ls\demo_09 try.xlsx')

if __name__=='__main__':
    spider = tengxun()
    spider.run()
  • 写回答

2条回答

  • 小黑LLB 2019-07-15 18:20
    关注

    item-line item-line--moddle => item-line item-line--middle
    但是数据还是不全,我也没办法了

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料