爬虫库之 requests_html,json

1. xpinyin 模块

定义:将汉字转换为拼音的的第三方库:xpinyin

安装:

python 复制代码
pip install xpinyin

导包:

python 复制代码
from xpinyin import Pinyin

案例:

1.

python 复制代码
#导入xpinyin模块
from xpinyin import Pinyin

#调用Pinyin类
py=Pinyin()
#get_pinyin 获取中文拼音   默认分隔符  -
print(py.get_pinyin("凤凌"," "))
#tone_marks="marks"  带音标的拼音
print(py.get_pinyin("凤凌"," ",tone_marks="maks"))
print(py.get_initial("凌"))
print(py.get_initials("凤凌"))

2.

python 复制代码
from xpinyin import Pinyin
import requests

py=Pinyin()

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

word='美女'

#获取请求网址
url=f'https://699pic.com/tupian/{py.get_pinyin(word,"")}.html'
print(url)

with open(f"{word}.html","w",encoding="utf8") as f:
    f.write(requests.get(url,headers=headers).text)
    

3.获取同音汉字

python 复制代码
from xpinyin import Pinyin
import requests

py=Pinyin()

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

words=["星星","猩猩"]

for word in words:
    #获取中文对应的Id
    pinyin_id_url=f'https://699pic.com/search/getKwInfo?kw={word}'
    response=requests.get(pinyin_id_url,headers=headers)
    print(response.json())  #字符串转换成字典数据
    pinyin_id=response.json().get('data').get('pinyin')

    #获取请求网址
    url=f'https://699pic.com/tupian/{py.get_pinyin(word,"")}.html'
    print(url)


    with open(f"{word}.html","w",encoding="utf8") as f:
        f.write(requests.get(url,headers=headers).text)

2.requests_html模块

2.1 requests_html模块介绍:
  • requests_html是比较新的爬虫库,作者和requests是同一个作者
  • 我们可以在安装的时候看到他安装了lxml,requests,bs4...我们常用的解析和爬取的库都封装在他里面
  • 用法和requests.session实例化的对象用法一模一样,也会自动保存返回信息
  • 相比requests,他多了对于response.html这个属性
  • 注意点:默认是带了有UA的,无需额外添加
2.2 requests_html于requests的区别:
python 复制代码
import requests
import requests_html

session = requests.session()
# requests_html
html_session = requests_html.HTMLSession()

url="http://httpbin.org/get"

response = session.get(url)
print(response.text)


#区别一:提交给服务器的user-Agent伪装成浏览器的user-Agent
html_response=html_session.get(url)
print(html_response.text)

#requests_html 模块中 默认A
html_response=html_session.get(url)
print(html_response.text)

#区别二:为response响应增加了属性名为html对象属性
baidu_url="https://www.baidu.com"
response1=session.get(baidu_url)
html_response1=html_session.get(baidu_url)
html=html_response1.html
print(html,type(html))

3. json 模块

3.1 爬虫中的数据分类:

结构化数据:json,xml等

​ 处理方式:直接转化为python类型

非结构化数据:html

​ 处理方式:正则表达式,xpath,选择器

JSON:全称JavaScript Object Notation,即JavaScript对象标记法,这种数据格式的语法规则是参考JavaScript对象,但是他能被很多语言去使用,是前后端进行交互的一种非常常见的格式,不同语言都有处理JSON格式的数据方法,而在python当初处理JSON则是使用json模块。

**XML:**全称EXtensible Markup Language,即可扩展标记语言,XML是一种标记语言,跟HTML类似,但是XML中的所有标签都没有含义,需要自行去定义标签,XML被设计出来是为了传输数据使用的。在JSON格式出现前,XML也是一种万维网中传输数据使用的最为广泛的一种,但是JSON格式出现后,JSON格式相对于XML的比重反而更多,XML更多是作为配置文件使用。

**HTML:**全称HyperText Markup Language,即超文本标记语言,我们见到的网页数据都是放在各种HTML标签里面的,通过一些解析库,可以从HTML标签获取到我们想要的数据。

3.2 案例:

python 复制代码
import json
#load:从文件中读取json字符串,转换成python字典
print("\n load:从文件中读取json字符串,转换成python字典")
with open("info.json","r",encoding="utf8") as rf:
 data=json.load(rf)
 print(data,type(data))

#loads:将json字符串转换成python字典
print("\n 将json字符串转换成python字典")
json_data = '{"name":"ls","age":28}'
dict_data = json.loads(json_data)
print(dict_data,type(dict_data))

#dump:将python字典转换成json的字符串,并且写入到文件中
print("\n,dump:将python字典转换成json的字符串,并且写入到文件中")
dict_data={"name":"碎花","age":18}
with open("info.json","w",encoding="utf8") as wf:
 json.dump(dict_data,wf,ensure_ascii=False)


#dumps:将python字典转换成json字符串
print("\n dumps:将python字典转换成json字符串")
json_str=json.dumps(dict_data,ensure_ascii=False)
print(json_str,type(json_str))
相关推荐
shandianchengzi3 小时前
【开源工具】我做了一个周深歌曲淘汰赛生成器:挑选 128 首歌 PK,选出你的年度 TOP1
html·音乐·网页·世界杯
触底反弹12 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
TlSfoward20 小时前
抓包代理链路下的 TLS 指纹变化分析 TLSFOWARD抓包工具
数据库·爬虫·网络协议·搜索引擎·php
电商API_1800790524721 小时前
企业ERP进销存场景|京东商品详情接口自动同步方案|凭证鉴权批量调用技术实操
大数据·运维·人工智能·爬虫·数据挖掘·网络爬虫
两点王爷1 天前
一个快速加载面和多面数据的html测试页面(可以导出geojson数据文件)
前端·html·gis
CedarQR1 天前
万字长文:从零在 RK3588 上部署 PaddleSpeech 中文 TTS 全流程(FastSpeech2 + HiFiGAN)
开发语言·c++·嵌入式硬件·ubuntu·json
拓海SEO外贸1 天前
Google 爬虫管理:抓取预算、404 与重定向
爬虫·搜索引擎·信息可视化·seo·跨境电商·独立站搭建·seo优化
深蓝电商API1 天前
浏览器缓存机制对爬虫有什么影响?
爬虫
Dontla1 天前
网站爬虫控制策略介绍(robots.txt、sitemap.xml、x-robots-tag、noindex、nofollow)网站索引
xml·爬虫·dubbo
杨超越luckly1 天前
Agent应用指南:长三角41城跨城人口流动OD数据分析
python·数据挖掘·数据分析·html·可视化