爬取汽车之家新闻图片的python爬虫代码
2024-09-06 00:20:45
import requests
from bs4 import BeautifulSoup
respone=requests.get('https://www.autohome.com.cn/news/')
respone.encoding='gbk'
# print(respone.text) soup=BeautifulSoup(respone.text,'html.parser')
div=soup.find(name='div',attrs={'id':'auto-channel-lazyload-article'})
li_list=div.find_all(name='li') i=1
for li in li_list:
print('pro:',i)
title=li.find(name='h3')
if not title:
continue
p=li.find(name='p')
a=li.find(name='a')
img=li.find(name='img') print(title.text)
print(p.text)
print('https:'+a.attrs.get('href'))
print('https:'+img.get('src')) #img.get==img.attrs.get #请求下载图片
src='https:'+img.get('src')
file_name=src.rsplit('/',maxsplit=1)[1]
with open(file_name,'wb') as f:
ret=requests.get(src)
f.write(ret.content)
最新文章
- android 两种实现计时器时分秒的实现,把时间放在你的手中~
- easyUI 复选框批量操作
- js反射机制
- 活动组件(三):Intent
- Func系列4:其他功能
- C\C++编程中:相对路径+绝对路径
- solr英文使用的基本分词器和过滤器配置
- HDU4553 约会安排
- PHP Sessions
- win7 安装 vagrant + centos + virtualbox
- webpack中dev-server不写contentBase时如何设置可以显示页面并且加载js
- 《java.util.concurrent 包源码阅读》27 Phaser 第一部分
- window 7 安装Jmeter并配置https录制脚本
- 【原创】Linux基础之curl
- vue中router使用keep-alive缓存页面的注意事项
- 搭建EOS环境
- 关于PHP程序员技术职业生涯规划
- 腾讯云Badjs镜像使用入门
- os模块学习+open行数
- Discuptor入门(二)-实例