php抓取网页json数据(Python网络爬虫findfind动态网页的两种技术:1.使用)
优采云 发布时间: 2021-12-15 17:19php抓取网页json数据(Python网络爬虫findfind动态网页的两种技术:1.使用)
由于主流的网站都使用JavaScript来展示网页内容,不像之前的静态网页简单爬行,使用JavaScript的时候很多内容没有出现在HTML源代码中,而是放在了HTML源代码中代码。一段JavaScript代码,最后呈现的数据是由JavaScript提取出来的,提取出服务器返回的数据,加载到源代码中进行呈现。因此,抓取静态网页的技术可能无法正常工作。因此,我们需要使用两种技术进行动态网页爬取:
1.通过浏览器评论元素分析真实网页地址;
2.使用selenium模拟浏览器。
我们这里先介绍第一种方法。
以《Python Web Crawler:从入门到实践》作者的个人博客评论为例。网址:1)“抓包”:找到真实数据地址
右键单击“检查”,单击“网络”,然后选择“js”。刷新页面,选择数据列表?回调...这个页面刷新时返回的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”]定位。
观察到实际数据地址中的偏移量是页数。