zoukankan      html  css  js  c++  java
  • py 爬取汽车之家新闻案例

    ``` 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) #保存字节内容 ``` > 更多精彩文章请关注 [王明昌博客](https://www.wangmingchang.com)
  • 相关阅读:
    JAVA的向上转型 和 向下转型
    Mobx 常用函数整理
    HTTP错误代码对应
    D3官网上的力导图
    用D3画简单的力导图
    nodejs 爬图片
    nodejs 爬热搜
    canvas 图片旋转 导出下载
    canvas 电子签名
    canvas 音乐跳动效果
  • 原文地址:https://www.cnblogs.com/wmc1125/p/11878675.html
Copyright © 2011-2022 走看看