鱼C论坛

 找回密码
 立即注册
查看: 944|回复: 1

爬取ip地址出问题

[复制链接]
发表于 2019-3-2 21:37:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我检查了一下,是Xpath语法从第二页开始爬取为空,但是同样的语法单独用在爬取第二页可以正常爬取到数据,
一旦用在for循环之中就不能正常运行爬取,求怎么解决

  1. import requests
  2. import os
  3. from lxml import etree


  4. class Get():
  5.     def __init__(self):
  6.         self.headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
  7.         self.url = "https://www.kuaidaili.com/free/inha/%d/"
  8.         self.ip_list = []
  9.         self.port_list = []
  10.         self.type_list = []
  11.         self.time_list = []
  12.         self.daili_list = []
  13.         self.detection_list = []
  14.     def start(self):
  15.         for e in range(1,5):
  16.             response = requests.get(self.url % e, headers = self.headers)
  17.             html = etree.HTML(response.text)
  18.             result = html.xpath("//tbody/tr//td/text()")
  19.             for i in range(len(result)):
  20.                 if i % 7 == 0:
  21.                     self.ip_list.append(result[i])
  22.                 elif i % 7 == 1:
  23.                     self.port_list.append(result[i])
  24.                 elif i % 7 == 3:
  25.                     self.type_list.append(result[i])
  26.                 elif i % 7 == 6:
  27.                     self.time_list.append(result[i])
  28.                     
  29.         for i in range(len(self.ip_list)):
  30.             self.daili_list.append("""+ self.type_list[i] + """ + ":" +\
  31.                         """+self.ip_list[i] +":" +self.port_list[i] + """)
  32.             self.detection_list.append("         最近检测时间" +\
  33.                         self.time_list[i])
  34.         with open("iplist.txt", "w") as f:   
  35.             for i in range(len(self.daili_list)):
  36.                 f.write(self.daili_list[i] + self.detection_list[i] + "\n")
  37.         return self.daili_list


  38. if __name__ == "__main__":
  39.     get = Get()
  40.     get.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-3 12:11:17 | 显示全部楼层
没人吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 06:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表