鱼C论坛

 找回密码
 立即注册
查看: 1059|回复: 4

[已解决]爬虫问题

[复制链接]
发表于 2019-3-3 20:10:59 | 显示全部楼层 |阅读模式

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

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

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()
复制代码

最佳答案
2019-3-5 02:23:00
等待下就可以抓取到
  1. #_*_coding:utf-8_*_
  2. # @Time      :2019/3/4--23:33
  3. # @Author   : Stubbron
  4. # @Email    : 1263270345@qq.com
  5. # @File     :test.py
  6. import requests
  7. import os,time
  8. from lxml import etree
  9. from fake_useragent import UserAgent
  10. ua = UserAgent()
  11. Headers = {
  12.     'User-Agent': ua.random
  13. }
  14. agent_pool = []
  15. def get2_proxy(page):
  16.     "快代理"
  17.     global agent_pool
  18.     url = "https://www.kuaidaili.com/free/inha/{}/".format(page)
  19.     try:
  20.         html = etree.HTML(requests.get(url, headers=Headers).text)
  21.         for each in html.xpath('//table[@class="table table-bordered table-striped"]//tr')[1:]:
  22.             ip = each.xpath('./td[1]/text()')[0] + ":" + each.xpath('./td[2]/text()')[0]
  23.             if ip:
  24.                 agent_pool.append(ip)
  25.     except:
  26.         print("get2_proxy抓取失败")
  27.     return agent_pool

  28. for i in range(1,3):
  29.     time.sleep(5)  #等待5秒
  30.     get2_proxy(i)
  31.     print(len(agent_pool))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-3 23:24:59 | 显示全部楼层
你的访问速度太快了,这个网站的服务器使用了反爬虫的机制,高频访问直接返回了503状态码。
你可以使用time模块的time.sleep()函数在每个次请求网页以后暂停几秒钟
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-3 23:26:08 | 显示全部楼层
所以这不是你xpath查找的问题,是你第二次请求网页的时候就没有收到数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-4 22:49:32 | 显示全部楼层
zero_sunshine 发表于 2019-3-3 23:26
所以这不是你xpath查找的问题,是你第二次请求网页的时候就没有收到数据

那该怎么解决?换ip可以吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-5 02:23:00 | 显示全部楼层    本楼为最佳答案   
等待下就可以抓取到
  1. #_*_coding:utf-8_*_
  2. # @Time      :2019/3/4--23:33
  3. # @Author   : Stubbron
  4. # @Email    : 1263270345@qq.com
  5. # @File     :test.py
  6. import requests
  7. import os,time
  8. from lxml import etree
  9. from fake_useragent import UserAgent
  10. ua = UserAgent()
  11. Headers = {
  12.     'User-Agent': ua.random
  13. }
  14. agent_pool = []
  15. def get2_proxy(page):
  16.     "快代理"
  17.     global agent_pool
  18.     url = "https://www.kuaidaili.com/free/inha/{}/".format(page)
  19.     try:
  20.         html = etree.HTML(requests.get(url, headers=Headers).text)
  21.         for each in html.xpath('//table[@class="table table-bordered table-striped"]//tr')[1:]:
  22.             ip = each.xpath('./td[1]/text()')[0] + ":" + each.xpath('./td[2]/text()')[0]
  23.             if ip:
  24.                 agent_pool.append(ip)
  25.     except:
  26.         print("get2_proxy抓取失败")
  27.     return agent_pool

  28. for i in range(1,3):
  29.     time.sleep(5)  #等待5秒
  30.     get2_proxy(i)
  31.     print(len(agent_pool))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-13 03:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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