首页 > 编程语言 >python爬取彼岸桌面4K壁纸

python爬取彼岸桌面4K壁纸

时间:2023-03-13 13:22:19浏览次数:43  
标签:etree img python 爬取 headers html 4K requests response

import requests 
from lxml import etree
import os

url = 'https://pic.netbian.com/4kmeinv/index.html'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}

if not os.path.exists('./picture'):
    os.makedirs('./picture')

response = requests.get(url, headers=headers)
etree_html = etree.HTML(response.text)

lis = etree_html.xpath('//*[@id="main"]/div[3]//a/@href')

for li in lis:
    response = requests.get(url="https://pic.netbian.com/" + li, headers=headers)
    response.encoding = "gbk"
    html = etree.HTML(response.text)
    address = html.xpath('//*[@id="img"]/img/@src')[0]
    name = html.xpath('//*[@id="img"]/img/@alt')[0] + ".jpg"

    img_path = 'E:\\python\\01初识爬虫,requests使用\\picture\\' + name
    with open(img_path, 'wb') as f:
        response = requests.get(url="https://pic.netbian.com/" + address, headers=headers).content
        f.write(response)
        print("OK")
f.close()

 

标签:etree,img,python,爬取,headers,html,4K,requests,response
From: https://www.cnblogs.com/shuxi/p/17211011.html

相关文章

  • python爬虫案列03,爬取58二手房信息
    importrequestsfromlxmlimportetreeurl="https://fy.58.com/ershoufang/?PGTID=0d100000-0091-53ca-4993-576198ca62e3"headers={"user-agent":"Mozilla/5.......
  • Python常见面试题013.请说出下面的代码返回结果是什么?
    013.请说出下面的代码返回结果是什么?*的坑;简单题参考:https://docs.python.org/zh-cn/3.9/library/stdtypes.html#typesseq示例代码lists=[[]]*3lists[0].appen......
  • python爬取免费高匿爬虫ip
    现在大部分门户网站都会做一些反爬虫的策略,对于长期做数据爬虫的程序猿来说那是深有体会。其实说白了就是用同一个地址频繁去爬虫一个网页很容易导致ip被关进小黑屋,为了安......
  • python函数
    函数:是组织好的,可重复使用的,用来实现特定功能的代码段。函数的定义:def函数名(传入参数):函数体return返回值 函数的调用:函数名(参数)注意事项:参数不......
  • python的正则表达式匹配C类地址
    简介如果你不理解Python正则表达式,可以参考以下步骤:学习正则表达式的基本语法。正则表达式是一种描述字符串模式的语言,通过一些特殊字符和语法规则来描述字符串的组成......
  • python 当前时间多加一天、一小时、一分钟
    importdatetime#获取当前时间print(datetime.datetime.now())#2017-07-1515:01:24.619000#格式化时间print(datetime.datetime.now().strftime("%Y-%m-%d%H:%M......
  • Python3爬虫教程之ADSL拨号爬虫ip池的使用
    在我之前做爬虫经常需要维护自己的爬虫ip池,他可以挑选出很多有用的爬虫地址,因为不是专业的而且这些爬虫ip通常是公共爬虫ip,所以可用率不是太高,而且这样类型的地址很大情况下......
  • Centos 7升级原python 2.7.5至Python 3.7
    1.安装编译环境包(防止出现安装错误)yuminstallgcc-c++gccmakecmakezlib-develbzip2-developenssl-develncurse-devel-y2.在线下载Python3.7源码包#进入tmp目......
  • python数据分析与挖掘实战第七章
    #代码7-1数据探索importpandasaspddatafile='data3/air_data.csv'#航空原始数据,第一行为属性标签resultfile='data3/explore.csv'#数据探索结果表data=......
  • 启动anaconda命题提示符之后输入python之后,出现报错: UnicodeDecodeError: ‘gbk‘ cod
      打开路径中的history.py文件,在它的的82行中添加:encoding='utf-8',然后保存history.py文件。最后重启anaconda命题提示符之后输入python即可正常运行......