模拟百度进行图片搜索,有问题可以留言

2022-10-17,

模拟百度进行图片搜索

import requests
import re
#分析页面我们先在百度图片搜索里面随笔输入一个'背景图片'获得链接如下
#https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=背景图片
#word为我们查找的内容


# data = input('请输入你要搜索的内容')
# rp = requests.get(f'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word={data}')
# rp.encoding = rp.apparent_encoding
# print(rp.text)
#试了一下很明显内容是错误的,主体很多内容没有


#这时候我们加headers
headers = {
    'user-agent': 'mozilla/5.0 (windows nt 6.1; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/75.0.3770.90 safari/537.36'
}
#user-agent是对于我们来源的进行伪装

data = input('请输入你要搜索的内容')
rp = requests.get(f'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=0&xthttps=111111&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word={data}',headers=headers)
rp.encoding = rp.apparent_encoding
data = '"middleurl":"(.*?)",'
# print(rp.text)
#对于url查找我们一定不要看原网页上的,要看访问请求下来的text,他可能会相比于原来网页会少了点前缀之类的,但是肯定会有一部分内容一样,绝对能发现的.
url = re.findall(data,rp.text,re.s)
print(url)


#下一步对就是对爬取的url再进行访问可以将文件保存至本地,具体可以参考博客
https://www.cnblogs.com/pythonywy/p/10856508.html

《模拟百度进行图片搜索,有问题可以留言.doc》

下载本文的Word格式文档,以方便收藏与打印。