爬取界面中“贵州茅台”相关新闻的标题、网址和日期。

1 需求

爬取界面中“贵州茅台”相关新闻的标题、网址和日期。

2 代码实现

import re

from selenium import webdriver


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)

# 获取网页源代码
url = 'https://a.jiemian.com/index.php?m=search&a=index&msg=贵州茅台&type=news'
browser.get(url=url)
data = browser.page_source

# 获取网页标题
p_title = '<div class="news-header">.*?title="(.*?)">'
title = re.findall(p_title, data)

# 获取网页网址
p_href = '<div class="news-view.*?href="(.*?)" target="_blank"'
href = re.findall(p_href, data)

# 获取新闻日期
p_date = '<span class="date">(.*?)</span>'
date = re.findall(p_date, data)

# 数据整理
for index in range(len(title)):
    print(str(index + 1) + "." + title[index] + "(" + date[index] + ")")
    print(href[index])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值