抓取网页数据违法吗(Python网络爬虫findfind动态网页的两种技术:1.使用)

优采云 发布时间: 2022-02-02 18:15

  抓取网页数据违法吗(Python网络爬虫findfind动态网页的两种技术:1.使用)

  由于主流的网站都使用JavaScript来展示网页内容,不像之前简单的静态网页爬取,在使用JavaScript的时候,很多内容并没有出现在HTML源码中,而是放在了HTML中源代码。一段JavaScript代码,最终渲染出来的数据是通过JavaScript提取服务器返回的数据,加载到源代码中进行渲染。因此,用于抓取静态网页的技术可能无法正常工作。因此,我们需要使用两种技术进行动态网页抓取:

  1.通过浏览器检查元素解析真实网页地址;

  2.一种使用 selenium 模拟浏览器的方法。

  我们这里先介绍第一种方法。

  以爬取《Python Web爬虫:从入门到实践》一书作者的个人博客评论为例。URL: 1) "capture": 找到真实的数据地址

  右键单击“检查”,单击“网络”,然后选择“js”。刷新页面,选择数据列表?callback....页面刷新时返回的js文件。在右侧,再次选择页眉。如图所示:

  

  其中,Request URL 是真实的数据地址。

  在此状态下滚动鼠标滚轮以发现 User-Agent。

  

  2)相关代码:

  import requests

import json

headers={\'User-Agent\':\'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\'}

link="https://api-zero.livere.com/v1/comments/list?callback=jQuery112405600294326674093_1523687034324&limit=10&offset=2&repSeq=3871836&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1523687034329"

r=requests.get(link,headers=headers)

# 获取 json 的 string

json_string = r.text

json_string = json_string[json_string.find(\'{\'):-2]

json_data=json.loads(json_string)

comment_list=json_data[\'results\'][\'parents\']

for eachone in comment_list:

message=eachone[\'content\']

print(message)

  输出是:

  现在死在了4.2节上,页面评论是有的,但是XHR里没有东西啊,这是什么情况?有解决的大神吗?

为何静态网页抓取不了?

奇怪了,我按照书上的方法来操作,XHR也是空的啊

XHR没有显示任何东西啊。奇怪。

找到原因了

caps["marionette"] = True

作者可以解释一下这句话是干什么的吗

我用的是 pycham IDE,按照作者的写法写的,怎么不行

对火狐版本有要求吗

4.3.1 打开Hello World,代码用的作者的,火狐地址我也设置了,为啥运行没反应

from selenium import webdriver

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

caps = webdriver.DesiredCapabilities().FIREFOX

caps["marionette"] = False

binary = FirefoxBinary(r\'C:\Program Files\Mozilla Firefox\firefox.exe\')

#把上述地址改成你电脑中Firefox程序的地址

driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps)

driver.get("http://www.santostang.com/2017/03/02/hello-world/")

我是番茄

为什么刷新没有XHR数据,评论明明加载出来了

  代码分析:

  1) 对于代码 json_string.find() api 解析为:

  Docstring:

S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,

such that sub is contained within S[start:end]. Optional

arguments start and end are interpreted as in slice notation.

Return -1 on failure.

Type: method_descriptor

  所以代码 json_string.find(\'{\') 返回“{”在 json_string 字符串中的索引位置。

  2)如果在代码中加一行代码print json_string,会输出句子(由于输出内容太多,只截取开头和结尾,关键位置用红色标出) :

  /**/ typeof jQuery112405600294326674093_1523687034324 === \'function\' && jQuery112405600294326674093_1523687034324({"results":{"parents":[{"replySeq":33365104,"name":"骨犬","memberId":"B9E06FBF9013D49CADBB5B623E8226C8","memberIcon":"http://q.qlogo.cn/qqapp/101256433/B9E06FBF9013D49CADBB5B623E8226C8/100","memberUrl":"https://qq.com/","memberDomain":"qq","good":0,"bad":0,"police":0,"parentSeq":33365104,"directSeq":0,"shortUrl":null,"title":"Hello world! - 数据科学@唐松

Santos","site":"http://www.santostang.com/2017/03/02/hello-world/","email":null,"ipAddress":"27.210.192.241","isMobile":"0","agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.8.3.16721","septSns":null,"targetService":null,"targetUserName":null,"info1":null,"info2":null,"info3":null,"image1":null,"image2":null,"image3":null,"link1":null,"link2":null,"link3":null,"isSecret":0,"isModified":0,"confirm":0,"subCount":1,"regdate":"2018-01-01T06:27:50.000Z","deletedDate":null,"file1":null,"file2":null,"file3":null,"additionalSeq":0,"content":"现在死在了4.2节上,页面评论是有的,但是XHR里没有东西啊,这是什么情况?有解决的大神吗?"

。。。。。。。。。 tent":"我的也是提示火狐版本不匹配,你解决了吗","quotationSeq":null,"quotationContent":null,"consumerSeq":1020,"livereSeq":28583,"repSeq":3871836,"memberGroupSeq":26828779,"memberSeq":27312353,"status":0,"repGroupSeq":0,"adminSeq":25413747,"deleteReason":null,"sticker":0,"version":null}],"quotations":[]},"resultCode":200,"resultMessage":"Okay, livere"});

  从上面的输出,我们可以看出在代码中加入 json_string = json_string[json_string.find(\'{\'):-2] 的重要性。

  如果不添加json_string.find(\'{\'),则结果不是合法的json格式,无法成功形成json文件;如果没有截到倒数第二位,则结果收录冗余);它也不构成合法的 json 格式。

  3)对于代码comment_list=json_data[\'results\'][\'parents\']和message=eachone[\'content\']中括号内字符串类型的标签定位,可以是发现在上面2)中的关键部分进行了搜索,也就是截取后的合法json文件中同时收录了“results”和“parents”,所以用两个括号逐步定位,又因为我们爬的是评论,它的内容在json文件的“content”标签中,所以使用[“content”]来定位。

  观察到实际数据地址中的偏移量是页码。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线