欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3爬蟲爬取百姓網(wǎng)列表并保存為json功能示例【基于request、lxml和json模塊】

 更新時間:2018年12月05日 10:59:01   作者:包子源  
這篇文章主要介紹了Python3爬蟲爬取百姓網(wǎng)列表并保存為json功能,涉及Python基于request、lxml和json模塊的Request請求與響應數(shù)據(jù)處理相關操作技巧,需要的朋友可以參考下

本文實例講述了Python3爬蟲爬取百姓網(wǎng)列表并保存為json功能。分享給大家供大家參考,具體如下:

python3爬蟲之爬取百姓網(wǎng)列表并保存為json文件。這幾天一直在學習使用python3爬取數(shù)據(jù),今天記錄一下,代碼很簡單很容易上手。

首先需要安裝python3。如果還沒有安裝,可參考本站python3安裝與配置相關文章。

首先需要安裝requestslxmljson三個模塊

需要手動創(chuàng)建d.json文件

代碼

import requests
from lxml import etree
import json
#構造頭文件,模擬瀏覽器訪問
url="http://xian.baixing.com/meirongfuwu/"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36','referer':url}
response=requests.get(url,headers=headers)
body=response.text #獲取網(wǎng)頁內容
html=etree.HTML(body,etree.HTMLParser())
gethtml=html.xpath('//div[contains(@class,"media-body-title")]')
# 存儲為數(shù)組list
jsondata = []
for item in gethtml:
  jsonone={}
  jsonone['title']=item.xpath('.//a[contains(@class,"ad-title")]/text()')[0]
  jsonone['url']=item.xpath('.//a[contains(@class,"ad-title")]/attribute::href')[0]
  jsonone['phone']=item.xpath('.//button[contains(@class,"contact-button")]/attribute::data-contact')[0]
  jsondata.append(jsonone)
# 保存為json
with open("./d.json",'w',encoding='utf-8') as json_file:
  json.dump(jsondata,json_file,ensure_ascii=False)

結果

PS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python正則表達式用法總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論