日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python爬虫之Ajax动态加载数据抓取--豆瓣电影/腾讯招聘

發(fā)布時(shí)間:2024/1/1 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫之Ajax动态加载数据抓取--豆瓣电影/腾讯招聘 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

動(dòng)態(tài)加載數(shù)據(jù)抓取-Ajax

  • 特點(diǎn)
1、右鍵 -> 查看網(wǎng)頁(yè)源碼中沒(méi)有具體數(shù)據(jù) 2、滾動(dòng)鼠標(biāo)滑輪或其他動(dòng)作時(shí)加載
  • 抓取
1、F12打開(kāi)控制臺(tái),頁(yè)面動(dòng)作抓取網(wǎng)絡(luò)數(shù)據(jù)包 2、抓取json文件URL地址 # 控制臺(tái)中 XHR :異步加載的數(shù)據(jù)包 # XHR -> QueryStringParameters(查詢參數(shù))

豆瓣電影數(shù)據(jù)抓取案例

  • 目標(biāo)
1、地址: 豆瓣電影 - 排行榜 - 劇情 2、目標(biāo): 電影名稱(chēng)、電影評(píng)分
  • F12抓包(XHR)
1、Request URL(基準(zhǔn)URL地址) :https://movie.douban.com/j/chart/top_list? 2、Query String(查詢參數(shù)) # 抓取的查詢參數(shù)如下: type: 13 interval_id: 100:90 action: '' start: 0 limit: 用戶輸入的電影數(shù)量
  • json模塊的使用
1、json.loads(json格式的字符串):把json格式的字符串轉(zhuǎn)為python數(shù)據(jù)類(lèi)型 # 示例 html = json.loads(res.text) print(type(html))
  • 代碼實(shí)現(xiàn)
import requests import jsonclass DoubanSpider(object):def __init__(self):self.url = 'https://movie.douban.com/j/chart/top_list?'self.headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}# 獲取頁(yè)面def get_page(self,params):res = requests.get(url=self.url,params=params,headers=self.headers,verify=True)res.encoding = 'utf-8'# json.loads() josn格式->Python格式html = res.json()self.parse_page(html)# 解析并保存數(shù)據(jù)def parse_page(self,html):# html為大列表 [{電影1信息},{},{}]for h in html:# 名稱(chēng)name = h['title'].strip()# 評(píng)分score = float(h['score'].strip())# 打印測(cè)試print([name,score])# 主函數(shù)def main(self):limit = input('請(qǐng)輸入電影數(shù)量:')params = {'type' : '24','interval_id' : '100:90','action' : '','start' : '0','limit' : limit}# 調(diào)用函數(shù),傳遞params參數(shù)self.get_page(params)if __name__ == '__main__':spider = DoubanSpider()spider.main()

騰訊招聘案例

  • URL地址及目標(biāo)
  • 確定URL地址及目標(biāo)

    1、URL: 百度搜索騰訊招聘 - 查看工作崗位 2、目標(biāo): 職位名稱(chēng)、工作職責(zé)、崗位要求
  • F12抓包

  • 一級(jí)頁(yè)面json地址(index變,timestamp未檢查)

  • https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1563912271089&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex={}&pageSize=10&language=zh-cn&area=cn
  • 二級(jí)頁(yè)面地址(postId在變,在一級(jí)頁(yè)面中可拿到)
  • https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1563912374645&postId={}&language=zh-cn
    • 具體代碼實(shí)現(xiàn)
    import requests import json import time import randomclass TencentSpider(object):def __init__(self):self.headers = {'User-Agent':'Mozilla/5.0'}self.one_url = 'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1563912271089&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex={}&pageSize=10&language=zh-cn&area=cn'def get_page(self,url):res = requests.get(url,headers=self.headers)res.encoding = 'utf-8'# json.loads()把json格式的字符串轉(zhuǎn)為python數(shù)據(jù)類(lèi)型html = json.loads(res.text)return htmldef parse_one_page(self,html):job_info = {}for job in html['Data']['Posts']:job_info['job_name'] = job['RecruitPostName']job_info['job_address'] = job['LocationName']# 拿postid為了拼接二級(jí)頁(yè)面地址post_id = job['PostId']# 職責(zé)和要求(二級(jí)頁(yè)面)# 得到二級(jí)頁(yè)面鏈接two_url = 'https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1563912374645&postId={}&language=zh-cn'.format(post_id)# 發(fā)請(qǐng)求解析job_info['job_duty'],job_info['job_requirement'] = self.parse_two_page(two_url)print(job_info)def parse_two_page(self,two_url):html = self.get_page(two_url)# 職責(zé)job_duty = html['Data']['Responsibility']# 要求job_requirement = html['Data']['Requirement']return job_duty,job_requirementdef main(self):for index in range(1,11):url = self.one_url.format(index)one_html = self.get_page(url)self.parse_one_page(one_html)time.sleep(random.uniform(0.5,1.5))if __name__ == '__main__':spider = TencentSpider()spider.main()

    附git地址:https://github.com/RyanLove1/spider_code

    補(bǔ)充:

    控制臺(tái)抓包

    • 打開(kāi)方式及常用選項(xiàng)
    1、打開(kāi)瀏覽器,F12打開(kāi)控制臺(tái),找到Network選項(xiàng)卡 2、控制臺(tái)常用選項(xiàng)1、Network: 抓取網(wǎng)絡(luò)數(shù)據(jù)包1、ALL: 抓取所有的網(wǎng)絡(luò)數(shù)據(jù)包2、XHR:抓取異步加載的網(wǎng)絡(luò)數(shù)據(jù)包3、JS : 抓取所有的JS文件2、Sources: 格式化輸出并打斷點(diǎn)調(diào)試JavaScript代碼,助于分析爬蟲(chóng)中一些參數(shù)3、Console: 交互模式,可對(duì)JavaScript中的代碼進(jìn)行測(cè)試 3、抓取具體網(wǎng)絡(luò)數(shù)據(jù)包后1、單擊左側(cè)網(wǎng)絡(luò)數(shù)據(jù)包地址,進(jìn)入數(shù)據(jù)包詳情,查看右側(cè)2、右側(cè):1、Headers: 整個(gè)請(qǐng)求信息General、Response Headers、Request Headers、Query String、Form Data2、Preview: 對(duì)響應(yīng)內(nèi)容進(jìn)行預(yù)覽3、Response:響應(yīng)內(nèi)容

    python中正則處理headers和formdata

    1、pycharm進(jìn)入方法 :Ctrl + r ,選中 Regex 2、處理headers和formdata(.*): (.*)"$1": "$2", 3、點(diǎn)擊 Replace All

    總結(jié)

    以上是生活随笔為你收集整理的python爬虫之Ajax动态加载数据抓取--豆瓣电影/腾讯招聘的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。