python 广告牌_Python从百度搜索广告牌抓取新闻并自动推送到电子邮件中,爬取,风云榜,邮箱...

本文将使用Python爬取百度新闻搜索指数排名前50的新闻,并通过服务器运行,每天定时发送到指定邮箱。

先上代码:

# -*- coding:utf-8 -*-

import requests,os,re,email,smtplib

EMAIL_ADDRESS='****@hotmail.com'

EMAIL_PASSWORD='***'

s=smtplib.SMTP(host='smtp.office365.com',port=587)

s.starttls()

s.login(EMAIL_ADDRESS,EMAIL_PASSWORD)

print('Connected~')

url='http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b1'

r=requests.get(url)

txt=r.content.decode('GBK')

pt=re.compile('href_top=".*?">(.*?)

title=re.findall(pt,txt)

pt1=re.compile('href="(.*?)" href_top')

urls=re.findall(pt1,txt)

#print(len(urls))

pt2=re.compile('icon-....">(.*?)

rise=re.findall(pt2,txt)

#print(title,rise,urls)

i=50

mss=''

for x in range(i):

ma=(str(x+1),title[x],rise[x],urls[x])

ms=', '.join(ma)

mss+=(ms+'\n')

msgs=mss

msg=email.message_from_string(msgs)

msg['From']=EMAIL_ADDRESS

msg['To']=EMAIL_ADDRESS # add who need receive the eamil

s.sendmail(EMAIL_ADDRESS,EMAIL_PASSWORD,msg.as_string())

s.quit()

print('Your data sent to %s',EMAIL_ADDRESS)

主要使用了requests,smtplib, re,email模块,只需要30几行代码就可以实现。

接下来我说下怎么在服务器上设置:

crontab -e这个命令可以搜索了解下怎么使用的,

在我们服务器下面输入这个命令 crontab -e

* * * * * program

进入编辑模式按 i 键

30 07 * * * /usr/local/bin/python3 /usr/temp/baidu.py 每天早上7:30分运行一次

然后:wq 保存,就可以执行了。另外需要注意的是一定要写成绝对路径,这样保证程序能运行。

最后上一张截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里是一个简单的Python-Scrapy爬取百度搜索结果并对搜索结果进行分析的例子: 首先,我们需要安装Scrapy和lxml库。在命令行输入以下命令: ``` pip install scrapy pip install lxml ``` 然后,我们可以创建一个名为baidu_spider的新项目,并在项目创建一个名为baidu的新爬虫。在命令行输入以下命令: ``` scrapy startproject baidu_spider cd baidu_spider scrapy genspider baidu www.baidu.com ``` 现在,我们在baidu_spider/spiders/baidu.py文件编写我们的代码。我们将使用Scrapy的Selector来选择我们想要的数据。代码如下: ```python import scrapy class BaiduSpider(scrapy.Spider): name = "baidu" allowed_domains = ["www.baidu.com"] start_urls = ["http://www.baidu.com/s?wd=python"] def parse(self, response): # 获取搜索结果 results = response.xpath('//div[@class="result c-container "]') for result in results: # 获取标题和链接 title = result.xpath('.//h3/a/text()').extract_first().strip() link = result.xpath('.//h3/a/@href').extract_first() # 获取摘要 abstract = result.xpath('.//div[@class="c-abstract"]//text()').extract() abstract = "".join(abstract).strip() # 打印结果 print(title) print(link) print(abstract) ``` 在这个例子,我们首先定义了我们的爬虫的名称,允许的域名和起始URL。然后我们定义了一个parse函数来处理响应。在parse函数,我们使用XPath选择器来选择搜索结果。我们使用extract_first()和extract()方法来提取标题、链接和摘要。最后,我们打印了结果。 现在,我们可以在baidu_spider目录运行以下命令来运行我们的爬虫: ``` scrapy crawl baidu ``` 这将启动我们的爬虫并开始爬取百度搜索结果。在控制台,您应该能够看到我们的爬虫正在输出搜索结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值