资源简介
一个单文件爬虫,实现监听页面变化并发送邮件。
代码片段和文件信息
import urllib
from urllib import request
from bs4 import BeautifulSoup
from datetime import datetime
import random
import time
import pymysql
import smtplib
import sys
import requests
import json
from email.header import Header
from email.mime.text import MIMEText
# 连接mysql
def get_mysql():
db = pymysql.connect(host=‘localhost‘
port=3306
user=‘root‘ passwd=‘root‘
db=‘test‘ charset=‘utf8‘
cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()
return db cursor
# 爬虫
def get_spiderMsg():
host = {}
title = {}
lastest_title = {}
host[0] = ‘http://cjxy.hebtu.edu.cn/a/zxks/tzgg/index.html‘
title[0] = ‘自考实践通知‘
# 获取数据库中保存的最新标题
db mysql = get_mysql()
select_sql = “SELECT title FROM test where url = ‘“+host[0]+“‘ORDER BY updated_at desc“
mysql.execute(select_sql)
res = mysql.fetchone()
lastest_title[0] = ‘‘
if res:
lastest_title[0] = res[‘title‘];
# email相关
mail_host = “smtp.163.com“ # SMTP服务器
mail_user = “xxxxxxx“ # 用户名
mail_pass = “xxxxxxxxx“ # 授权密码,非登录密码
sender = ‘xxxxxx@163.com‘ # 发件人邮箱(最好写全 不然会失败)
receivers = [‘1111111111@qq.com‘] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
header_list = [‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.000‘
‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/57.0.2987.133 Safari/537.36‘
‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2759.400 QQBrowser/9.6.11220.400‘
‘Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko‘]
switch = True
while switch:
time.sleep(3)
sys.stdout.flush()
i = 0
range_header = random.randint(0 3)
user_agent = header_list[range_header]
accept = ‘text/htmlapplication/xhtml+xmlapplication/xml;q=0.9image/
相关资源
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
- Python串口通信(pyserial)
- python读取nfc卡
- django图片浏览+scrapy实现数据抓取功能
- jetbrains-agent.jar(pycharm破解包)
- 豆瓣爬虫;Scrapy框架
- 近百条常用有用的python语句
- Python数据结构.pdf60078
- python实现香农编码
- 自动绘制优美图案.py
- python批量获取网站上的视频地址
- Python求解非线性方程组的高效方法.
-
Python sc
ript for fix VxWorks SymTbl in IDA - Python黑魔法指南-v2.0
- 酷喵浏览器 python代码
- 用python导出oracle表百万表记录到EXCE
- Django博客+pyecharts动态折线图+mysql
- python wifi 破解(基于pywifi)
- Python面试大全(245道面试题)
- 中国城市经纬度爬虫.ipynb
- python抓取天地图瓦片
- 石头剪刀布python代码
- 100道Python练习题(附源码)
- python 语音提示( 基于SAPI.SPVOICE)
- scrapy框架爬取58同城数据
评论
共有 条评论