爬虫库之 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))
相关推荐
袁袁袁袁满2 小时前
OpenAI SDK集成亮数据网页解锁器实现自动化爬虫
爬虫·python·ai·网络爬虫·爬虫实战·自动化爬虫·ai爬虫
深蓝电商API2 小时前
Scrapy 自定义命令与扩展:打造专属爬虫工具
爬虫·python·scrapy
2501_9159090611 小时前
如何保护 iOS IPA 文件中资源与文件的安全,图片、JSON重命名
android·ios·小程序·uni-app·json·iphone·webview
组合缺一13 小时前
Json Dom 怎么玩转?
java·json·dom·snack4
seabirdssss13 小时前
《bootstrap is not defined 导致“获取配置详情失败”?一次前端踩坑实录》
前端·bootstrap·html
wtsolutions14 小时前
MCP Server Integration - JSON to Excel for AI and Automation
json·excel
\xin14 小时前
Fastjson 1.2.45仅JSON接口反序列化漏洞
安全·web安全·json
司机204815 小时前
将virtuoso原理图信息导出到json文件
json·github
我是伪码农16 小时前
轮播图案例
css·html·css3