py 爬取汽车之家新闻案例

代码溯影客
• 阅读 2061
import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.autohome.com.cn/news/")

# 1. content /text  的区别
# print(response.content)  # content 拿到的字节
response.encoding = 'gbk'
# print(response.text)  # text 拿到的文本信息


soup  = BeautifulSoup(response.text,'html.parser')

# tag = soup.find(id='auto-channel-lazyload-article')  # 找唯一的值,缩小范围
# h3 = tag.find(name='h3',class_ ='')  # class是关键词所以要加下划线, 或者使用下面的方式
# h3 = tag.find(name='h3',attrs= {'class':''})  #
# print(h3)

# 链式写法
li_list = soup.find(id='auto-channel-lazyload-article').find_all(name='li')

for li in li_list:
    title = li.find('h3') #获取标题
    if not title:# 如果为null,跳出
        continue
    title = title.text
    summary = li.find("p").text
    url = li.find("a").get('href')
    img = li.find("img").get('src')
    print(img)

    # 保存图片
    res = requests.get(img)
    file_name = "%s.jpg"%(title,)
    with open(file_name,'wb') as f:
        f.write(res.content) #保存字节内容
更多精彩文章请关注 王明昌博客
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Wesley13 Wesley13
3年前
java中比较两个时间的差值
项目背景1.某篇文稿的发布时间是publishDate,例如:2020072118:00:41。2.现要求判断该篇文稿的发布时间是否在近30天之内。publicstaticlongdayDiff(DatecurrentDate,DatepublishDate){LongcurrentTimecurrentDat
Wesley13 Wesley13
3年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Wesley13 Wesley13
3年前
MySQL总结(十一)子查询
!(https://oscimg.oschina.net/oscnet/upa344f41e81d3568e3310b5da00c57ced8ea.png)子查询1\.什么是子查询需求:查询开发部中有哪些员工selectfromemp;通
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
3年前
ES6之路第三篇:字符串的扩展
字符的Unicode表示法1"\u{20BB7}"2//"𠮷"34"\u{41}\u{42}\u{43}"5//"ABC"67lethello123;8hell\u{6F}//123910'\u{
Stella981 Stella981
3年前
Android默认字体ASCII码中可显示字符的平均灰度由小到大排序
如题:46,96,39,58,45,44,59,34,95,126,33,94,105,114,124,47,73,61,60,62,42,108,92,49,116,43,106,63,118,41,40,76,102,123,55,125,74,84,99,120,122,93,91,117,110,115,89,111,70,121,101,50,
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(