影816 2022-01-26 20:48 采纳率: 66.7%
浏览 1060
已结题

爬虫报错TypeError: stat: path should be string, bytes, os.PathLike or integer, not function,如何解决?

问题遇到的现象和发生背景

用PyCharm爬取https://www.dieniao.com的免费代理IP时,出现错误TypeError: stat: path should be string, bytes, os.PathLike or integer, not function。用的环境是Anaconda

问题相关代码,请勿粘贴截图
#免费获取代理IP
import requests
import urllib3
from lxml import etree
import pandas as pd
from sqlalchemy import false

ip_list = []                                                #创建保存IP地址的列表
urllib3.disable_warnings()

def get_ip(url,headers):
    #发送网络请求
    response = requests.get(url,headers=headers,verify = false)
    response.encoding = 'utf-8'                             #设置编码方式
    if response.status_code == 200:                         #判断请求是否成功
        html = etree.HTML(response.text)                    #解析HTML
        #获取所有带有IP的li标签
        li_all = html.xpath('//li[@class="f-list col-lg-12 col-md-12 col-sm-12 col-xs-12"]')
        for i in li_all:                                            #遍历每行内容
            ip = i.xpath('span[@class="f-address"]/text()')[0]  #获取IP
            port = i.xpath('span[@class="f-port"]/text()')[0]   #获取端口
            ip_list.append(ip+':'+port)                         #将IP与端口组合并添加至列表当中
            print('代理ip为:', ip, '对应端口为:', port)
#头部信息
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
if __name__ == '__main__':
    ip_table = pd.DataFrame(columns=['ip'])                     #创建临时表格数据
    for i in range(1,5):
        #获取免费代理IP的请求地址
        url = 'https://www.dieniao.com/FreeProxy/{page}.html'.format(page=i)
        get_ip(url,headers)
    ip_table['ip'] = ip_list                        #将提取的IP保存至Excel文件的IP列
    #生成xlsx文件
    ip_table.to_excel('ip.xlsx', sheet_name='data')

img

想让程序正常运行,爬取到IP

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 2月4日
      • 已采纳回答 1月27日
      • 创建了问题 1月26日

      悬赏问题

      • ¥15 用mysql做一个高校工资管理系统
      • ¥21 Multisim设计用三端集成稳压器设计稳压电路
      • ¥15 tp6,在iframe里的网址通过获取url参数失败
      • ¥15 打开opendaylight查看拓扑,为什么直接就显示了两个Switch,在mininet配置了一个topo,这两个Switch还在
      • ¥15 无法远程连接pgsql
      • ¥15 建立DLNM模型时显示基础基矩阵不一致
      • ¥15 贝叶斯重复测量方差分析
      • ¥15 c#五子棋为什么悔棋后全部的棋子都没了 但是数组中的棋子还在只是悔棋后剩余的棋子在窗口没显示
      • ¥15 c#如何将到毫秒的时间字符串转换成byte[8]
      • ¥100 能不能帮帮我,这个数据库我一直做不好