爬取在新浪财经中搜索“贵州茅台”得到的信息,包括新闻标题、网址、来源、日期。

1 需求

爬取在新浪财经中搜索“贵州茅台”(https://search.sina.com.cn/?q=贵州茅台&c=news&from=channel)得到的信息,包括新闻标题、网址、来源、日期。

2 代码实现

import requests
import re


# 获取网页源代码
url = 'https://search.sina.com.cn/?q=贵州茅台&c=news&from=channel'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
source = requests.get(url=url, headers=headers).text
# 获取网页图片的下载地址
p_img = ';" src="(.*?)" />'
img = re.findall(p_img, source)
# 获取网页标题
p_title = '.shtml" target="_blank">(.*?)</a>'
title = re.findall(p_title, source)
# 获取网页网址
p_href = '<h2><a href="(.*?)" target="_blank">'
href = re.findall(p_href, source)
# 获取来源
p_come = '<span class="fgray_time">(.*?)</span></h2>'
come = re.findall(p_come, source)
# 数据清理并存储
for index in range(len(title)):
    title[index] = re.sub('<.*?>', '', title[index]).strip()
    file = open('images/' + title[index] + ".jpg", 'wb')
    pic = requests.get(img[index])
    file.write(pic.content)
    file.close()
    print(str(index + 1) + "." + title[index] + "(" + href[index] + ")")
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值