鱼C论坛

 找回密码
 立即注册
查看: 2900|回复: 22

58同城的爬虫代码求助

[复制链接]
发表于 2018-12-26 11:09:37 | 显示全部楼层 |阅读模式

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

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

x
作业。。要求爬取58同城下面的租房信息。。但是发现标签定位时,爬取的信息不完整(一页有超过50条以上的信息,我只能每页爬35条下来)。。
求指点下问题出在哪里。。。
  1. import requests as res
  2. from bs4 import BeautifulSoup as bs
  3. import pandas as pd
  4. def Zf(page):
  5.     l=[['标题','户型','面积','地段','租金','发布时间','租房信息来源人','租房信息来源']]
  6.     for i in range(page):
  7.         url = 'https://fz.58.com/zufang/pn%s/?PGTID=0d300008-0013-0503-c723-fd9232df4484&ClickID=2'%i
  8.         head = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}
  9.         res1 = res.get(url ,headers =head)
  10.         soup = bs(res1.text,'lxml')
  11.         cont = soup.find('ul',class_='listUl').find_all('li',sortid ='1545753605000')
  12.         
  13.         
  14.         for i in cont:
  15.             tit = i.find('div',class_='des').find('a',class_='strongbox').text.replace(' ','').replace('\n','').replace('\xa0','') #标题
  16.             
  17.             house = i.find('p',class_='room strongbox').text.replace('\n','').replace('\xa0','')
  18.             housetype = house.split(' ')[0] #户型,将原先的房子信息切分
  19.             area = house[-10:].replace(' ','') #面积
  20.             add = i.find('p',class_='add').a.text.replace(' ','').replace('\n','').replace('\xa0','') #地段
  21.             from_pc = i.find('div',class_='jjr').text.replace(' ','').replace('\n','').replace('\xa0','')
  22.             time = i.find('div',class_='sendTime').text
  23.             
  24.             person = from_pc.split(':')[0]  #将经纪人及其信息切分
  25.             company = from_pc.split(':')[1]
  26.             
  27.             money = i.find('b',class_='strongbox').text.replace(' ','').replace('\n','').replace('\xa0','')
  28.             tot = [tit,housetype,area,add,money,time,person,company]
  29.             l.append(tot)
  30.         data = pd.DataFrame(l)
  31.         data.to_csv(r'D:\大数据学习资料\python学习\作业5\58.csv',mode = 'a',header = None)
  32. Zf(5)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-26 11:53:18 | 显示全部楼层
我这里一页有123个信息呃
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-26 13:32:28 | 显示全部楼层
wongyusing 发表于 2018-12-26 11:53
我这里一页有123个信息呃

不是吧。。那为什么我的代码一页只能爬35个下来?
哪里有错么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-26 13:54:24 | 显示全部楼层
老笨啊 发表于 2018-12-26 13:32
不是吧。。那为什么我的代码一页只能爬35个下来?
哪里有错么

可能是这个网站根据每个浏览器,请求参数来返回不同的html吧。  
你在浏览器上看到的是50个。  
然而返回给爬虫的html代码只能看到35个。  

你打印一下res1.text看一下就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-26 14:31:01 | 显示全部楼层
wongyusing 发表于 2018-12-26 13:54
可能是这个网站根据每个浏览器,请求参数来返回不同的html吧。  
你在浏览器上看到的是50个。  
然而返 ...

我没去数我看到的几个,肯定超过50个,只是只能返回35个。。
不知道什么原因。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-26 15:46:26 From FishC Mobile | 显示全部楼层
你检查一下class有没有class不一样的,我电脑断网了,看不了页面
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-26 16:16:48 | 显示全部楼层
你的cont里面只有35个数据啊,我看每一页好像有53个数据,你用这个标签去匹配  <div class="des">
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-26 18:42:34 | 显示全部楼层
海风zZ 发表于 2018-12-26 16:16
你的cont里面只有35个数据啊,我看每一页好像有53个数据,你用这个标签去匹配

你看我的tit,就是用的class_='des'匹配的啊。。
因为后面的租金不在这个标签下面,所以cont变量只能以li,sortid来定位
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-26 18:43:48 | 显示全部楼层
海风zZ 发表于 2018-12-26 16:16
你的cont里面只有35个数据啊,我看每一页好像有53个数据,你用这个标签去匹配

你看我的tit,就是用的class_='des'匹配的啊。。
因为后面的租金不在这个标签下面,所以cont变量只能以li,sortid来定位
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-26 18:44:26 | 显示全部楼层
wongyusing 发表于 2018-12-26 15:46
你检查一下class有没有class不一样的,我电脑断网了,看不了页面

我设置了的。。尽量确保定位的唯一性。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-26 19:33:21 | 显示全部楼层
你的第11行导致的

干嘛写个1545753605000哪里啊???
后面的都是1545653605000
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-27 10:18:56 | 显示全部楼层
老笨啊 发表于 2018-12-26 18:43
你看我的tit,就是用的class_='des'匹配的啊。。
因为后面的租金不在这个标签下面,所以cont变量只能以l ...

这是我改的代码,只爬取了标题,好像每页有133个,每次爬取的最后数据都不一样,我用网页看了,每次刷新之后都会变,
  1. url = 'https://fz.58.com/zufang/pn1/?PGTID=0d300008-0013-0503-c723-fd9232df4484&ClickID=2'
  2. head = {
  3.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}
  4. res1 = res.get(url, headers=head)
  5. soup = bs(res1.text, 'lxml')
  6. # print(type(soup))
  7. cont = soup.find_all('div',class_="des")
  8. # print(type(cont))
  9. # print(len(cont))
  10. for i in cont:
  11.     # print(type(i))
  12.     tit = i.find('a', class_='strongbox').text.replace(' ', '').replace('\n','').replace('\xa0', '')  # 标题
  13.     print(tit)
复制代码


好像数据保存的时候汉字会乱码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-27 11:39:34 | 显示全部楼层
wongyusing 发表于 2018-12-26 19:33
你的第11行导致的

干嘛写个1545753605000哪里啊???

大神,你怎么看出来的啊。。
我的sortid只看了第一个。。以为后面都是一样的了。。
这个li标签的属性就2个,logr和sortid...我只选了一个。。
如果是这样的话,那不是得多搞几个循环出来?? 就没法一次性爬一页的数据了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-27 11:42:32 | 显示全部楼层
海风zZ 发表于 2018-12-27 10:18
这是我改的代码,只爬取了标题,好像每页有133个,每次爬取的最后数据都不一样,我用网页看了,每次刷新 ...

汉字乱码,用encoding = apparent.encoding来解决。
tit可以爬完整,是因为你的des定位的问题,租金什么的数据,不在des下面。。所以要爬租金的话,还得往上找一层标签,到li那个去。。但是按照11#大神的说法,li的属性里,sortid的值并不都一样。。这样的话,不是得搞死。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-27 12:34:09 | 显示全部楼层
老笨啊 发表于 2018-12-27 11:42
汉字乱码,用encoding = apparent.encoding来解决。
tit可以爬完整,是因为你的des定位的问题,租金什么 ...

这是刚改的,你看一下,好像经纪人那一块会报错,因为有的房源是个人的就没有div', class_='jjr'这个标签是用的div', class_='geren'这个这边在处理一下就好了,你试试看,我这是只爬取第二页的53个数据
  1. url = 'https://fz.58.com/zufang/pn2/?PGTID=0d300008-0013-0503-c723-fd9232df4484&ClickID=2'
  2. head = {
  3.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36'}
  4. res1 = res.get(url, headers=head)
  5. soup = bs(res1.text, 'lxml')
  6. # print(type(soup))
  7. cont = soup.find('ul',class_="listUl").find_all('li')
  8. # print(type(cont))
  9. print(len(cont))
  10. for i in range(0,len(cont)-1):
  11.     # it = cont[i].find('a', class_='strongbox').text.replace(' ', '').replace('\n', '').replace('\xa0', '')  # 标题
  12.     # print(it)
  13.     house = cont[i].find('p', class_='room strongbox').text.replace('\n', '').replace('\xa0', '')
  14.     # print(house)
  15.     # area = house[-10:].replace(' ', '')  # 面积

  16.     # dd = cont[i].find('p', class_='add').a.text.replace(' ', '').replace('\n', '').replace('\xa0', '')  # 地段
  17.     # print(dd)
  18.     # time = cont[i].find('div', class_='sendTime').text
  19.     # print(time)
  20.     # from_pc =cont[i].find('div', class_='jjr').text.replace(' ', '').replace('\n', '').replace('\xa0', '')
  21.     # print(from_pc)
  22.     # person = from_pc.split(':')[0]  # 将经纪人及其信息切分
  23.     # company = from_pc.split(':')[1]
  24.     money =cont[i].find('b', class_='strongbox').text.replace(' ', '').replace('\n', '').replace('\xa0', '')
  25.     # print(money)
复制代码




老哥你弄好之后发一下代码,我看看乱码咋处理的学习一哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-27 12:39:23 | 显示全部楼层
为什么你们学bs4只学了find方法呢???
select方法不学,谁教的??
select比find方法简单,易懂。  
除了非常复杂的特殊需求才用find方法。   
大多数情况下select只要写两个标签就行了

  1. from bs4 import BeautifulSoup as bs
  2. def get_response(url):
  3.     headers = {
  4.         'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36",
  5.   
  6.     }
  7.     response = requests.get(url, headers) # 加上浏览器头,以防被禁
  8.     response.encoding = 'utf-8'      # 指定编码格式
  9.     #response.encoding = 'gbk'      # 指定编码格式
  10.     return response
  11. url = 'https://fz.58.com/zufang/pn0/?PGTID=0d300008-0013-0503-c723-fd9232df4484&ClickID=2'
  12. response = get_response(url)
  13. soup = bs(response.text,'lxml')
  14. content = soup.select('.listUl li')

  15. for each in content:
  16.     print(each)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-27 14:24:47 | 显示全部楼层
wongyusing 发表于 2018-12-27 12:39
为什么你们学bs4只学了find方法呢???
select方法不学,谁教的??
select比find方法简单,易懂。  

哈哈,肯定老师教的。。 每个人的风格不一样吧。。 我也不懂。。
我用了select命令后,发现报错啊,貌似要设置设个sortid才行,否则标签定位不唯一。。但是设置了id又只能爬35条。。。咋整。。
微信图片_20181227142231.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-27 16:12:48 | 显示全部楼层
讲一下这个网站的反爬策略吧。  
反爬策略是,实际上只有50条是真实数据。  
而html会返回123到133条li标签回来。  
用于混淆真实的数据。  

我们人类所能看到所显示的50行内容是通过一份680行的js代码加载出来的。  

加载过程是需要我们的曲奇(自行翻译成英文)中的bangbangid字段
在代码中的178行的getBaseInfo方法
然后通过getBaseInfo返回的内容
来返回到代码中的391行的listShowLog方法
来完成一系列操作后,返回真正的50行的信息。   
那份代码如下,里面英文和拼音混用,看得我头都大了
  1. (function(J) {
  2.     var d = J,
  3.         k = document,
  4.         q = d.location,
  5.         D = d.performance;
  6.     if (!d.TJ58) {
  7.         d.TJ58 = !0;
  8.         null == d.String.prototype.trim && (d.String.prototype.trim = function() {
  9.             return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "")
  10.         });
  11.         d.TJ58Obj = {};
  12.         var e = {
  13.                 curURL: q.href,
  14.                 referrer: k.referrer,
  15.                 protocol: q.protocol,
  16.                 window_size: k.documentElement.clientWidth + "x" + k.documentElement.clientHeight,
  17.                 screen_size: d.screen.width + "," + d.screen.height,
  18.                 domain: function() {
  19.                     var b = q.host.toLowerCase(),
  20.                         a = /.*?([^\.]+\.(com|org|net|biz|edu|cc)(\.[^\.]+)?)/;
  21.                     return a.test(b) ? "." + b.replace(a, "$1") : ""
  22.                 }(),
  23.                 navigation_type: function() {
  24.                     var b = "-1";
  25.                     try {
  26.                         D && D.navigation && (b = D.navigation.type)
  27.                     } catch (a) {
  28.                         return "-1"
  29.                     }
  30.                     return b
  31.                 }(),
  32.                 setCookie: function() {
  33.                     if (!k.cookie) return !1;
  34.                     var b = new Date;
  35.                     2 < arguments.length ? b.setTime(b.getTime() + 864E5 * arguments[2]) : b.setTime(b.getTime() + 18E5);
  36.                     2 <= arguments.length && (k.cookie = arguments[0] + "=" + escape(arguments[1]) + "; expires=" + b.toGMTString() + "; domain=" + e.domain + "; path=/")
  37.                 },
  38.                 getCookie: function(b) {
  39.                     if (!k.cookie) return "";
  40.                     var a;
  41.                     return (a = k.cookie.match(RegExp("(^| )" +
  42.                         b + "=([^;]*)(;|$)"))) ? unescape(a[2]) : ""
  43.                 },
  44.                 ajaxsend: function(b) {
  45.                     (new Image).src = b
  46.                 },
  47.                 getGTID: function(b, a, c) {
  48.                     function d(a, b, c) {
  49.                         a = ("" + a).length < b ? (Array(b + 1).join("0") + a).slice(-b) : "" + a;
  50.                         return -1 == c ? a : a.substring(0, c) + "-" + a.substring(c)
  51.                     }
  52.                     var e = {
  53.                         home: "1",
  54.                         index: "2",
  55.                         list: "3",
  56.                         detail: "4",
  57.                         post: "5",
  58.                         special: "6"
  59.                     };
  60.                     b = e[b] ? parseInt(e[b]).toString(16) : 0;
  61.                     a = a.split(",");
  62.                     a = a[a.length - 1];
  63.                     a = parseInt(a) ? parseInt(a).toString(16) : 0;
  64.                     c = c.split(",");
  65.                     c = c[c.length - 1];
  66.                     c = parseInt(c) ? parseInt(c).toString(16) : 0;
  67.                     e = (13).toString(16);
  68.                     return "llpccccc-tttt-txxx-xxxx-xxxxxxxxxxxx".replace(/x/g,
  69.                         function(a) {
  70.                             return (16 * Math.random() | 0).toString(16)
  71.                         }).replace(/ccccc/, d(a, 5, -1)).replace(/tttt-t/, d(c, 5, 4)).replace(/p/, d(b, 1, -1)).replace(/ll/, d(e, 2, -1))
  72.                 },
  73.                 setLocalStorage: function(b, a) {
  74.                     try {
  75.                         d.localStorage && d.localStorage.setItem(b, a)
  76.                     } catch (c) {}
  77.                 },
  78.                 getLocalStorage: function(b) {
  79.                     try {
  80.                         return d.localStorage ? d.localStorage.getItem(b) : ""
  81.                     } catch (a) {
  82.                         return ""
  83.                     }
  84.                 },
  85.                 getUUID: function(b) {
  86.                     var a = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(a) {
  87.                             var b = 16 * d.Math.random() | 0;
  88.                             return ("x" == a ? b : b & 3 | 8).toString(16)
  89.                         }),
  90.                         a = this.getCookie(b) || this.getLocalStorage(b) || a;
  91.                     this.setCookie(b, a, 365);
  92.                     this.setLocalStorage(b, a);
  93.                     return a
  94.                 },
  95.                 getRandom: function() {
  96.                     return d.Math.random()
  97.                 },
  98.                 bindElem: function(b, a, c, e, m) {
  99.                     if (d.$ && "string" == typeof b && "string" == typeof a && "function" == typeof c)
  100.                         if ("function" === typeof $(k).on) $(k).on(a, b, c);
  101.                         else "function" === typeof $(k).delegate ? $(k).delegate(b, a, c) : "function" === typeof $(b).bind && $(b).bind(a, c)
  102.                 },
  103.                 loadScript: function(b, a) {
  104.                     try {
  105.                         var c = k.createElement("script");
  106.                         c.type = "text/javascript";
  107.                         c.readyState ?
  108.                             c.onreadystatechange = function() {
  109.                                 if ("loaded" == c.readyState || "complete" == c.readyState) c.onreadystatechange = null, a && a()
  110.                             } : c.onload = function() {
  111.                                 a && a()
  112.                             };
  113.                         c.src = b;
  114.                         k.body.appendChild(c)
  115.                     } catch (d) {}
  116.                 }
  117.             },
  118.             f = {
  119.                 config: {
  120.                     trackLog: {
  121.                         server: "tracklog.58.com/pc/empty.js.gif",
  122.                         allParams: "site_name tag referrer post_count _trackParams userid smsc window_size _ga_utma trackURL rand_id".split(" "),
  123.                         uniqParams: ["tag", "rand_id"]
  124.                     },
  125.                     listShowLog: {
  126.                         server: "tracklog.58.com/PCv1/listshow/empty.js.gif",
  127.                         allParams: "tag bangbangid referrer site_name info_data trackURL rand_id".split(" "),
  128.                         uniqParams: ["tag", "info_data", "rand_id"]
  129.                     },
  130.                     listClickLog: {
  131.                         server: "tracklog.58.com/PCv1/listclick/empty.js.gif",
  132.                         allParams: "tag bangbangid referrer site_name info_data trackURL ClickID rand_id".split(" "),
  133.                         uniqParams: ["tag", "info_data", "rand_id"]
  134.                     },
  135.                     clickLog: {
  136.                         server: "tracklog.58.com/pc/click/empty.js.gif",
  137.                         allParams: "site_name tag from trackURL ClickID bangbangid referrer rand".split(" "),
  138.                         uniqParams: ["tag", "from", "rand"]
  139.                     },
  140.                     diaTrackLog: {
  141.                         server: "dialog.58.com/transfer",
  142.                         allParams: "DIATag tag referrer post_count _trackParams userid smsc window_size _ga_utma trackURL rand_id".split(" "),
  143.                         uniqParams: ["DIATag", "tag", "rand_id"]
  144.                     },
  145.                     diaClickLog: {
  146.                         server: "dialog.58.com/transfer",
  147.                         allParams: "DIATag from trackURL ClickID bangbangid referrer rand".split(" "),
  148.                         uniqParams: ["DIATag", "from", "rand"]
  149.                     },
  150.                     diaShowLog: {
  151.                         server: "dialog.58.com/transfer",
  152.                         allParams: "DIATag trackURL ClickID bangbangid referrer rand".split(" "),
  153.                         uniqParams: ["DIATag", "rand"]
  154.                     },
  155.                     gdtTrackLog: {
  156.                         server: "gdt.cm.58.com/gdtcm",
  157.                         allParams: ["city", "cate", "key", "plat"],
  158.                         uniqParams: ["city", "key", "plat"]
  159.                     },
  160.                     gdtTrackLog2: {
  161.                         server: "gdtcm.58.com/gdtcm",
  162.                         allParams: ["city", "cate", "key", "plat"],
  163.                         uniqParams: ["city", "key", "plat"]
  164.                     },
  165.                     actionLog: {
  166.                         server: "tracklog.58.com/PCv1/action/empty.js.gif",
  167.                         allParams: "site_name tag window_size action_data trackURL referrer rand_id".split(" "),
  168.                         uniqParams: ["tag", "action_data", "rand_id"]
  169.                     }
  170.                 },
  171.                 filterList: function(b) {
  172.                     var a = ".58.com.cn portal.58.com faw-vw-dasweltauto.58.com 5858.com lieche.58.com dict.58.com/xiaoqu about.58.com m.58.com/city.html lieche.m.58.com".split(" "),
  173.                         c;
  174.                     for (c in a)
  175.                         if (-1 !== b.indexOf(a[c])) return "YES";
  176.                     return "NO"
  177.                 },
  178.                 getBaseInfo: function() {
  179.                     var b = d.site_name || "58",
  180.                         a = d.encodeURIComponent(e.referrer),
  181.                         c = e.curURL,
  182.                         h = e.getUUID("58tj_uuid"),
  183.                         m = e.getCookie("bangbangid"),
  184.                         r = e.window_size,
  185.                         g = e.navigation_type,
  186.                         s = e.screen_size,
  187.                         p = d.____json4fe ? d.____json4fe : {},
  188.                         f = p._trackPagetype || "",
  189.                         k = p._trackURL || "",
  190.                         n = p._trackParams || [],
  191.                         l = p.GA_pageview || "",
  192.                         q = p.infoid || "",
  193.                         D = p.userid || "",
  194.                         O = p.smsc || "",
  195.                         p = p.sid || "",
  196.                         x = d._trackURL || k || "NA",
  197.                         v = {};
  198.                     try {
  199.                         v = "NA" === x ? {} : eval("(" + x + ")")
  200.                     } catch (S) {
  201.                         v = {}
  202.                     }
  203.                     var f = v.pagetype || f || d.page_type || "NA",
  204.                         k = v.post_count || d.post_count ||
  205.                         -1,
  206.                         l = v.Ga_pageview || l || "",
  207.                         y = v.cate || "",
  208.                         P = "" === y ? "" : y.split(",")[0],
  209.                         Q = "" === y && -1 === y.indexOf(",") ? "" : y.split(",")[1],
  210.                         G = v.area || "",
  211.                         M = "" === G ? "" : G.split(",")[0],
  212.                         N = e.getGTID(f, y, G),
  213.                         v = v.ROI || "",
  214.                         t = d._trackParams || n || [],
  215.                         z = [],
  216.                         n = "";
  217.                     if (t instanceof Array) {
  218.                         for (var n = 0, K = t.length; n < K; n++) t[n] && t[n].I && t[n].V && "string" === typeof t[n].V && z.push(t[n].I + ":" + t[n].V.replace(/\|/g, "*"));
  219.                         n = encodeURIComponent(z.join("@@"))
  220.                     }
  221.                     var z = (t = e.curURL.match(/[\?&]iuType=[a-z]*_[0-9]*/)) ? t[0].split("=")[1].split("_") : ["", ""],
  222.                         t = z[0],
  223.                         z = z[1],
  224.                         K = e.getCookie("als"),
  225.                         E = e.getCookie("utm_source"),
  226.                         R = e.getCookie("utm_campaign"),
  227.                         F = e.getCookie("spm"),
  228.                         A, C, H;
  229.                     "" != e.getCookie("new_session") ? (A = e.getCookie("init_refer"), C = "0") : (A = d.encodeURIComponent(e.referrer), C = "1");
  230.                     H = "" != e.getCookie("new_uv") ? parseInt(e.getCookie("new_uv")) + ("0" == C ? 0 : 1) : 1;
  231.                     e.setCookie("new_session", C);
  232.                     e.setCookie("new_uv", H, 365);
  233.                     var B = e.referrer.split("/")[2] || "",
  234.                         u = this.getValByUrl(e.curURL, "utm_source"),
  235.                         w = this.getValByUrl(e.curURL, "spm");
  236.                     if (!e.referrer && ("NA" != u || "NA" != w)) A =
  237.                         "", E = "NA" === u ? "" : u, F = "NA" === w ? "" : w;
  238.                     else if (!e.referrer && "NA" == u && "NA" == w && "1" === C) F = E = A = "";
  239.                     else if (B) {
  240.                         var L = !1;
  241.                         "cast.58.com" == B ? L = !0 : -1 === B.indexOf(".58.com") && -1 === B.indexOf(".5858.com") && -1 === B.indexOf(".58cdn.com.cn") && (L = !0);
  242.                         L && (A = J.encodeURIComponent(e.referrer), E = "NA" === u ? "" : u, F = "NA" === w ? "" : w)
  243.                     }
  244.                     e.setCookie("utm_source", E);
  245.                     e.setCookie("spm", F);
  246.                     e.setCookie("init_refer", A);
  247.                     var B = "1.1.1.1.1." + H,
  248.                         u = [],
  249.                         w = x.indexOf("{"),
  250.                         g = {
  251.                             GTID: N,
  252.                             infoid: q,
  253.                             infotype: t,
  254.                             usertype: z,
  255.                             als: K,
  256.                             utm_source: E,
  257.                             utm_campaign: R,
  258.                             spm: F,
  259.                             new_session: C,
  260.                             init_refer: A,
  261.                             new_uv: H,
  262.                             UUID: h,
  263.                             bangbangid: m,
  264.                             navtype: g,
  265.                             sc: s,
  266.                             sid: p
  267.                         },
  268.                         I;
  269.                     for (I in g) g.hasOwnProperty(I) && u.push("'" + I + "':'" + g[I] + "'");
  270.                     u.join(",");
  271.                     x = "NA" !== x && -1 !== w ? x.substring(0, w + 1) + u + "," + x.substring(w + 1) : "{" + u + "}";
  272.                     return {
  273.                         site_name: b,
  274.                         referrer: a,
  275.                         UUID: h,
  276.                         bangbangid: m,
  277.                         pagetype: f,
  278.                         post_count: k,
  279.                         cate: y,
  280.                         cate1: P,
  281.                         cate2: Q,
  282.                         area: G,
  283.                         area1: M,
  284.                         city: M,
  285.                         GTID: N,
  286.                         ClickID: 1,
  287.                         ROI: v,
  288.                         curURL: c,
  289.                         _trackParams: n,
  290.                         userid: D,
  291.                         smsc: O,
  292.                         window_size: r,
  293.                         trackURL: x,
  294.                         Ga_pageview: l,
  295.                         _ga_utma: B,
  296.                         ClickIDPlus: function() {
  297.                             this.ClickID += 1
  298.                         }
  299.                     }
  300.                 },
  301.                 getValByUrl: function(b,
  302.                     a) {
  303.                     var c;
  304.                     c = b.match(RegExp("[&?]" + a + "=([^&|^#]*)"));
  305.                     return c instanceof Array ? c[1] : "NA"
  306.                 },
  307.                 sendLog: function(b, a) {
  308.                     var c = this.baseInfo,
  309.                         d = this.config[b];
  310.                     if (b && d && a && "object" === typeof a) {
  311.                         for (var m = [], r = d.allParams, g = 0, s = r.length; g < s; g++) m.push(r[g] + "=" + (a[r[g]] || c[r[g]] || ""));
  312.                         e.ajaxsend(e.protocol + "//" + d.server + "?" + m.join("&"))
  313.                     }
  314.                 },
  315.                 trackLog: function() {
  316.                     var b = this.baseInfo;
  317.                     this.sendLog("trackLog", {
  318.                         tag: "pvstatall",
  319.                         rand_id: e.getRandom()
  320.                     });
  321.                     if ("list" === b.pagetype || "detail" === b.pagetype) {
  322.                         var a = b.Ga_pageview.indexOf("?key="),
  323.                             a = -1 !== a ? b.Ga_pageview.substring(a + 5) : "";
  324.                         "https:" == e.protocol ? this.sendLog("gdtTrackLog2", {
  325.                             city: b.area,
  326.                             key: a,
  327.                             plat: "PC"
  328.                         }) : this.sendLog("gdtTrackLog", {
  329.                             city: b.area,
  330.                             key: a,
  331.                             plat: "PC"
  332.                         })
  333.                     }
  334.                 },
  335.                 clickLog: function(b) {
  336.                     var a = "",
  337.                         a = null != b && "from=" === b.substring(0, 5) ? b.replace("from=", "") : "default&" + b;
  338.                     this.sendLog("clickLog", {
  339.                         tag: "pvsiters",
  340.                         from: a,
  341.                         rand: e.getRandom()
  342.                     });
  343.                     setTimeout("GCIDPlus()", 300)
  344.                 },
  345.                 listClickLog: function() {
  346.                     var b = this,
  347.                         a = this.baseInfo;
  348.                     if (d.$ && "NA" !== a.pagetype && "NA" !== a.trackURL) {
  349.                         var c = {
  350.                                 list: 1,
  351.                                 jianli_list: 1,
  352.                                 xiaoqu: 1,
  353.                                 qijiandian: 1,
  354.                                 item: 1,
  355.                                 xinchong: 1
  356.                             },
  357.                             h = function() {
  358.                                 if ("function" == typeof $(this).parents) {
  359.                                     var c = "",
  360.                                         c = $(this).parents("[logr]"),
  361.                                         g = [],
  362.                                         d = "",
  363.                                         h = c.attr("logr").split("_"),
  364.                                         m = c.attr("_pos"),
  365.                                         f = c.attr("sortid"),
  366.                                         k = c.attr("infoproperty"),
  367.                                         l = h[h.length - 1];
  368.                                     g.push(h[0], h[1], h[2], h[3]);
  369.                                     l && (l = l.replace("ses^", "ses:"), d += l);
  370.                                     l = "";
  371.                                     l = "function" == typeof $("[logr]").index ? $("[logr]").index(c) + 1 : c.attr("pos");
  372.                                     d = d + (f ? "@sortid:" + f : "") + (l ? "@pos:" + l : "");
  373.                                     d += m ? "@npos:" + m : "";
  374.                                     d += k ? "@infoproperty:" + k : "";
  375.                                     "" != d && (0 === d.indexOf("@") &&
  376.                                         (d = d.substring(1)), g.push(d));
  377.                                     c = g.join("_");
  378.                                     "NO" == b.filterList(a.curURL) && -1 != a.curURL.indexOf(".58.com") && (g = $(this).attr("href") || "#", -1 != g.indexOf("javascript:") || "#" == g.substring(0, 1) || "NO" != b.filterList(g) || "/" != g.substring(0, 1) && -1 == g.indexOf(".58.com") || g.match(/[\?&]iuType=/) || $(this).attr("href", g.trim() + (-1 == g.indexOf("?") ? "?" : "&") + "iuType=" + h[0] + "_" + h[1]));
  379.                                     b.sendLog("listClickLog", {
  380.                                         tag: "pclistclick",
  381.                                         info_data: c,
  382.                                         rand_id: e.getRandom()
  383.                                     });
  384.                                     setTimeout("GCIDPlus()", 300)
  385.                                 }
  386.                             },
  387.                             m = $("[tongji_label=listclick]");
  388.                         m && 0 < m.length ? $("[logr] [tongji_label=listclick]").bind("click", h) : 1 === c[a.pagetype] ? ($("[logr] a.t").bind("click", h), "12" === a.cate2 ? ($("[logr] a.jjh_img").bind("click", h), $("[logr] .img a").bind("click", h)) : "187" === a.cate1 || "42270" === a.cate1 ? $("[logr]").find("a:first").bind("click", h) : "9225" === a.cate1 || "13952" === a.cate1 ? $("[logr] a.fl").bind("click", h) : $("[logr] .img a").bind("click", h)) : "chexing" === a.pagetype && ($("[logr] a.at").bind("click", h), $("[logr] .tdimg a").bind("click", h))
  389.                     }
  390.                 },
  391.                 listShowLog: function() {
  392.                     var b =
  393.                         this.baseInfo;
  394.                     if (d.$ && "list" === b.pagetype) {
  395.                         for (var a = [], c = $("[logr]"), b = b.trackURL.length + b.referrer.length, h = 0, m = c.length; h < m; h++)
  396.                             if ($(c[h]).attr("logr") && "function" == typeof $(c[h]).attr) {
  397.                                 var f = [],
  398.                                     g = "",
  399.                                     s = $(c[h]).attr("logr").split("_"),
  400.                                     p = $(c[h]).attr("_pos"),
  401.                                     k = h + 1,
  402.                                     q = $(c[h]).attr("sortid"),
  403.                                     n = $(c[h]).attr("infoproperty"),
  404.                                     l = s[s.length - 1];
  405.                                 f.push(s[0], s[1], s[2], s[3]);
  406.                                 l && (l = l.replace("ses^", "ses:"), g += l);
  407.                                 g += q ? "@sortid:" + q : "";
  408.                                 g += k ? "@pos:" + k : "";
  409.                                 g += p ? "@npos:" + p : "";
  410.                                 g += n ? "@infoproperty:" + n : "";
  411.                                 "" != g && (0 === g.indexOf("@") &&
  412.                                     (g = g.substring(1)), f.push(g));
  413.                                 f = f.join("_");
  414.                                 curLogrLen = f.length;
  415.                                 g = a.join(",");
  416.                                 4096 < b + curLogrLen + g.length && (this.sendLog("listShowLog", {
  417.                                     tag: "pclistshow",
  418.                                     info_data: g,
  419.                                     rand_id: e.getRandom()
  420.                                 }), a = []);
  421.                                 a.push(f)
  422.                             }
  423.                         0 != a.length && this.sendLog("listShowLog", {
  424.                             tag: "pclistshow",
  425.                             info_data: a.join(","),
  426.                             rand_id: e.getRandom()
  427.                         })
  428.                     }
  429.                 },
  430.                 bindTongji_tag: function() {
  431.                     if (d.$) {
  432.                         var b = this;
  433.                         e.bindElem("[tongji_tag]", "click", function() {
  434.                             var a = $(this).attr("tongji_tag"),
  435.                                 c = $(this).text().trim();
  436.                             b.clickLog("from=" + a + "&text=" + encodeURIComponent(c) +
  437.                                 "&tongji_type=tongji_tag")
  438.                         }, b)
  439.                     }
  440.                 },
  441.                 bindTongji_id: function() {
  442.                     if (d.$) {
  443.                         var b = this;
  444.                         e.bindElem("[tongji_id]", "click", function(a) {
  445.                             var c = a.srcElement ? a.srcElement : a.target;
  446.                             "A" == c.tagName.toUpperCase() && (a = $(c).attr("href") || "#", c = $(c).text(), -1 == a.indexOf("javascript:") && "#" != a.substring(0, 1) && b.clickLog("from=" + $(this).attr("tongji_id") + "&text=" + encodeURIComponent(c) + "&to=" + encodeURIComponent(a) + "&tongji_type=tongji_id"))
  447.                         }, b)
  448.                     }
  449.                 },
  450.                 diaShowLog: function(b) {},
  451.                 bindAlsTag: function() {
  452.                     if (!e.getCookie("als") && d.$ && "function" ===
  453.                         typeof $("body").one) $("body").one("mouseover", function() {
  454.                         e.setCookie("als", "0", 365)
  455.                     });
  456.                     e.getCookie("isSpider") && e.setCookie("isSpider", "", 0)
  457.                 },
  458.                 bindHomeHeatMap: function() {
  459.                     var b = this,
  460.                         a = this.baseInfo;
  461.                     d.$ && "home" === a.pagetype && e.bindElem("[href]", "click", function(a) {
  462.                         var d = $(this).attr("href"),
  463.                             e = $(this).text().trim(),
  464.                             f = $(this).attr("tongji_tag") || "NA",
  465.                             g = $(this).offset().top,
  466.                             s = $(this).offset().left,
  467.                             k = a.pageX;
  468.                         a = a.pageY;
  469.                         b.clickLog("from=heatmap:" + encodeURIComponent(d) + "&tagX=" + s + "&tagY=" + g + "&mouseX=" + k + "&mouseY=" +
  470.                             a + "&text=" + encodeURIComponent(e) + "&tongji_tag=" + f)
  471.                     }, b)
  472.                 },
  473.                 insertMiGuan: function() {
  474.                     try {
  475.                         var b = "default";
  476.                         switch (this.baseInfo.cate1) {
  477.                             case "9224":
  478.                             case "9225":
  479.                             case "13941":
  480.                             case "13952":
  481.                                 b = "yewu";
  482.                                 break;
  483.                             case "1":
  484.                                 b = "ershoufang";
  485.                                 break;
  486.                             case "5":
  487.                                 b = "shouji";
  488.                                 break;
  489.                             case "832":
  490.                                 b = "dog";
  491.                                 break;
  492.                             case "4":
  493.                                 b = "ershouche";
  494.                                 break;
  495.                             default:
  496.                                 b = "shenghuo"
  497.                         }
  498.                         var a = Math.ceil(1E14 * Math.random()),
  499.                             c = document.getElementsByTagName("body")[0],
  500.                             d = document.createElement("div");
  501.                         d.id = "addInfo";
  502.                         d.style.display = "none";
  503.                         var f = document.createElement("a");
  504.                         f.href = e.protocol + "//tracklog.58.com/detail/pc/" + b + "/" + a + "x.shtml";
  505.                         f.text = "推荐信息";
  506.                         d.appendChild(f);
  507.                         c.appendChild(d)
  508.                     } catch (k) {}
  509.                 },
  510.                 bindAddGTIDtoURL: function() {
  511.                     var b = this,
  512.                         a = this.baseInfo;
  513.                     d.$ && e.bindElem("a", "click", function(c) {
  514.                         if ("NO" == b.filterList(a.curURL) && -1 != a.curURL.indexOf(".58.com") && (c = $(this).attr("href") || "#", -1 == c.indexOf("javascript:") && "#" != c.substring(0, 1) && "NO" == b.filterList(c) && ("/" == c.substring(0, 1) || -1 != c.indexOf(".58.com"))))
  515.                             if (c.match(/[\?&]ClickID=\d*/)) $(this).attr("href",
  516.                                 c.replace(/ClickID=\d*/, "ClickID=" + a.ClickID));
  517.                             else {
  518.                                 var d = c.indexOf("?"); - 1 != d && -1 != c.substring(d).indexOf("statmark=t") && -1 != c.substring(d).indexOf("#") ? (d = c.indexOf("statmark=t"), $(this).attr("href", c.substring(0, d + 10) + "&PGTID=" + a.GTID + "&ClickID=" + a.ClickID + c.substring(d + 10))) : $(this).attr("href", c.trim() + (-1 == d ? "?" : "&") + "PGTID=" + a.GTID + "&ClickID=" + a.ClickID)
  519.                             }
  520.                     }, b, a)
  521.                 },
  522.                 ActionObj: {
  523.                     data: []
  524.                 },
  525.                 actionFilter: function() {
  526.                     pagetypeArr = "home index list special jianli_index jianli_list reg PC_regist_sj".split(" ");
  527.                     var b = this.baseInfo,
  528.                         a = !1,
  529.                         d;
  530.                     for (d in pagetypeArr)
  531.                         if (b.pagetype == pagetypeArr[d]) {
  532.                             a = !0;
  533.                             break
  534.                         }
  535.                     return a ? !0 : !1
  536.                 },
  537.                 actionLog: function() {
  538.                     var b = this;
  539.                     b.actionFilter() && (e.bindElem("body", "click", function(a) {
  540.                         var c = b.ActionObj.data,
  541.                             h = k.body.scrollWidth || "",
  542.                             m = k.body.scrollHeight || "";
  543.                         a = a || d.event;
  544.                         var r = k.documentElement.scrollLeft || k.body.scrollLeft,
  545.                             g = k.documentElement.scrollTop || k.body.scrollTop,
  546.                             r = a.pageX || a.clientX + r || "-1";
  547.                         a = a.pageY || a.clientY + g || "-1";
  548.                         r = Math.floor(r);
  549.                         a = Math.floor(a);
  550.                         c.push("CLICK_" + h + "_" + m + "_" +
  551.                             r + "_" + a);
  552.                         c instanceof Array && 5 <= c.length && (c = c.join(","), f.ActionObj.data = [], b.sendLog("actionLog", {
  553.                             tag: "pcaction",
  554.                             action_data: c,
  555.                             rand_id: e.getRandom()
  556.                         }))
  557.                     }), d.$ && $(d).unload(function() {
  558.                         var a = b.ActionObj;
  559.                         (a = a ? a.data : "") && a instanceof Array && 0 < a.length && (a = a.join(","), f.ActionObj.data = [], b.sendLog("actionLog", {
  560.                             tag: "pcaction",
  561.                             action_data: a,
  562.                             rand_id: e.getRandom()
  563.                         }))
  564.                     }))
  565.                 },
  566.                 performanceLog: function() {
  567.                     var b = this.baseInfo,
  568.                         a = !1,
  569.                         c = "home index list special post detail".split(" "),
  570.                         e;
  571.                     for (e in c)
  572.                         if (b.pagetype == c[e]) {
  573.                             a = !0;
  574.                             break
  575.                         }
  576.                     a && d.$ && d.performance && d.performance.timing && $(function() {
  577.                         function a(b, d, c) {
  578.                             return "number" === typeof b && "number" === typeof d ? (b -= d, b = Math.floor(0 > b ? -1 : b), -1 == b && c ? c : b) : -1
  579.                         }

  580.                         function b(c) {
  581.                             var e = d.performance.timing,
  582.                                 e = {
  583.                                     loadPage: 0 == e.navigationStart ? a(e.loadEventEnd, e.fetchStart, c) : a(e.loadEventEnd, e.navigationStart, c),
  584.                                     domReady: a(e.domComplete, e.responseEnd, c),
  585.                                     redirect: a(e.redirectEnd, e.redirectStart, c),
  586.                                     lookupDomain: a(e.domainLookupEnd, e.domainLookupStart, c),
  587.                                     ttfb: 0 == e.navigationStart ? a(e.responseStart,
  588.                                         e.fetchStart, c) : a(e.responseStart, e.navigationStart, c),
  589.                                     request: a(e.responseEnd, e.requestStart, c),
  590.                                     loadEvent: a(e.loadEventEnd, e.loadEventStart, c),
  591.                                     appcache: a(e.domainLookupStart, e.fetchStart, c),
  592.                                     unloadEvent: a(e.unloadEventEnd, e.unloadEventStart, c),
  593.                                     connect: a(e.connectEnd, e.connectStart, c),
  594.                                     DOMContentLoaded: a(e.domContentLoadedEventEnd, e.fetchStart, c)
  595.                                 };
  596.                             c = [];
  597.                             for (var f in e) e.hasOwnProperty(f) && c.push("'" + f + "':'" + e[f] + "'");
  598.                             c.join(",");
  599.                             f = getTrackURL();
  600.                             e = f.indexOf("{");
  601.                             f = "NA" !== f && -1 !== e ? f.substring(0, e + 1) +
  602.                                 c + "," + f.substring(e + 1) : "{" + c + "}";
  603.                             c = [];
  604.                             c.push("site_name=58");
  605.                             c.push("tag=performance");
  606.                             c.push("referrer=" + J.encodeURIComponent(document.referrer));
  607.                             c.push("trackURL=" + f);
  608.                             c.push("rand_id=" + d.Math.random());
  609.                             c = q.protocol + "//tracklog.58.com/PCv1/performance/empty.js.gif?" + c.join("&");
  610.                             (new Image).src = c;
  611.                             d.TJ58Obj.send = !0;
  612.                             clearInterval(d.TJ58Obj.PFORMINTERVAL)
  613.                         }
  614.                         d.TJ58Obj.PFORMCOUNT = -1;
  615.                         d.TJ58Obj.send = !1;
  616.                         $(d).unload(function() {
  617.                             d.TJ58Obj.send || b("close_" + Math.floor(d.performance.now()))
  618.                         });
  619.                         d.TJ58Obj.PFORMINTERVAL =
  620.                             setInterval(function() {
  621.                                 d.TJ58Obj.PFORMCOUNT++;
  622.                                 6 < d.TJ58Obj.PFORMCOUNT && clearInterval(d.TJ58Obj.PFORMINTERVAL);
  623.                                 try {
  624.                                     0 < d.performance.timing.loadEventEnd ? b() : 6 <= d.TJ58Obj.PFORMCOUNT && b("TIMEOUT_" + Math.floor(d.performance.now()))
  625.                                 } catch (a) {
  626.                                     clearInterval(d.TJ58Obj.PFORMINTERVAL)
  627.                                 }
  628.                             }, 500)
  629.                     })
  630.                 },
  631.                 DMloadByTag: function() {
  632.                     try {
  633.                         var b = this.getValByUrl(e.curURL, "dm-statistic-detect"),
  634.                             a = e.getCookie("dm-statistic-detect");
  635.                         if (b = ("NA" == b ? "" : b) || a || "")
  636.                             if (d.TJ58ecdata = {}, d.TJ58ecdata.platform = "58PC", "https:" != q.protocol) switch (b) {
  637.                                 case "2":
  638.                                     e.loadScript(q.protocol +
  639.                                         "//stat.58corp.com/static/js/referrer_alert.js");
  640.                                     break;
  641.                                 case "3":
  642.                                     e.loadScript(q.protocol + "//stat.58corp.com/static/js/referrer_visual.js")
  643.                             }
  644.                     } catch (c) {}
  645.                 }
  646.             };
  647.         f.baseInfo = f.getBaseInfo();
  648.         f.trackLog();
  649.         f.listShowLog();
  650.         f.listClickLog();
  651.         f.bindAlsTag();
  652.         f.bindTongji_tag();
  653.         f.bindTongji_id();
  654.         f.bindHomeHeatMap();
  655.         f.bindAddGTIDtoURL();
  656.         f.insertMiGuan();
  657.         f.actionLog();
  658.         d.clickLog = function(b) {
  659.             f.clickLog(b)
  660.         };
  661.         d.showLog = function(b) {
  662.             f.diaShowLog(b)
  663.         };
  664.         d.GCIDPlus = function() {
  665.             f.baseInfo.ClickIDPlus()
  666.         };
  667.         d.ajaxlogr = function(b) {};
  668.         d.getGTID =
  669.             function() {
  670.                 return f.baseInfo.GTID
  671.             };
  672.         d.getTrackURL = function() {
  673.             return f.baseInfo.trackURL
  674.         };
  675.         d._gaq = d._gaq || [];
  676.         f.performanceLog();
  677.         f.DMloadByTag()
  678.     }
  679. })(window);
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-27 16:41:40 | 显示全部楼层
wongyusing 发表于 2018-12-27 16:12
讲一下这个网站的反爬策略吧。  
反爬策略是,实际上只有50条是真实数据。  
而html会返回123到133条li标 ...

我被你惊到了。。。很是不明觉厉。。
但是,能否解决下我目前的问题呢? 还是说无法解决?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-27 17:24:57 | 显示全部楼层
老笨啊 发表于 2018-12-27 16:41
我被你惊到了。。。很是不明觉厉。。
但是,能否解决下我目前的问题呢? 还是说无法解决?

除非你会翻译这段js,不然的话只能获取到一堆垃圾无效的数据。  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-12 16:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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