爬虫库之 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))
相关推荐
Venuslite2 天前
从 Unexpected token < 到 Extra data:一次讲清 JSON 解析错误的排查思路
json
ZhengEnCi4 天前
Q02-Vue-React-index.html完全指南
vue.js·react.js·html
牧艺5 天前
HTML-in-Canvas 深度解析:让 Canvas 真正「吃上」HTML 这碗饭
前端·html·canvas
爱勇宝5 天前
我给自己做了一个新标签页:不登录、不打扰、打开就能用
前端·html·浏览器
越努力越幸运666 天前
多模态代码调试实战:Gemini3.5 精准捕获 HTML 隐性语法
html
Caco_D7 天前
一行代码抓遍全网 20 个热榜!Aneiang.Pa 4.0 发布 — 极简 .NET 爬虫库
爬虫·.net
疯狂SQL8 天前
手写高性能在线 JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
typescript·json·next.js·web worker·前端性能优化·esbuild·源码实战
anOnion10 天前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
米丘10 天前
微前端之 Web Components 完全指南
微服务·html
太岁又沐风12 天前
复现并修掉ART hook框架 Pine 调用原方法时的偶发 SIGSEGV
爬虫