Python 调取图灵机器人接口实现AI对话

import requests import json userid = str(‘iamdu’) # 1 可以替换成任何长度小于32的字符串哦 apikey = str(‘A’) # 这里的A,记得替换成你自己的apikey哦~ # 创建post函数 def robot(content): # 图灵api api = r’http://openapi.tuling123.com/openapi/api/v2′ # 创建post提交的数据 data = { “perception”: { “inputText”: { “text”: content } }, “userInfo”: { “apiKey”: apikey, “userId”: userid, } } # 转化为json格式 jsondata = json.dumps(data) # 发起post请求 response = r…

Read More

Python 调取有道翻译接口实现在线翻译功能

有道翻译有反爬虫机制,它使用了加密技术。如果你的程序报错,你可以通过搜索、查阅资料找到解决方案:尝试把访问的网址中“/translate_o”中的“_o”删除。服务器返回的内容,是json的格式。我们可以用处理列表、处理字典的手段来提取翻译。 import requests,json #调用了两个模块。requests负责上传和下载数据,json负责解析。 while True: word = input(‘你想翻译什么呀?’) url=’http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule’ #使用post需要一个链接。 data={‘i’: word, ‘from’: ‘AUTO’, ‘to’: ‘AUTO’, ‘smartresult’: ‘dict’, ‘client’: ‘fanyidesk…

Read More

Python 模拟登陆给wordpress博客文章发表评论

import requests #引入requests。 url = ‘ https://wordpress-edu-3autumn.localprod.oc.forchange.cn/wp-login.php’ #把请求登录的网址赋值给url。 headers = { ‘User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36′ } #加请求头,前面有说过加请求头是为了模拟浏览器正常的访问,避免被反爬虫。 data = { ‘log’: ‘spiderman’, #写入账户 ‘pwd’: ‘crawler334566’, #写入密码 ‘wp-submit’: ‘登录’, ‘redirect_…

Read More

Python抓取知乎作者主页文章

import requests # 使用headers是一种习惯 headers={‘user-agent’:’Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36′} url=’https://www.zhihu.com/api/v4/members/zhang-jia-wei/articles?’ # 建立一个空列表,以待写入数据 articlelist=[] # 设置offset的起始值为第一页的值:10 offset=10 while True: # 封装参数 params={ ‘include’:’data[*].comment_count,suggest_edit,is_normal,thum…

Read More

Python 配合openpyxl模块 把抓取到的数据写入xlsx EXCEL表格

import requests,openpyxl # 创建工作薄 wb=openpyxl.Workbook() # 获取工作薄的活动表 sheet=wb.active # 工作表重命名 sheet.title=’lyrics’ sheet[‘A1′] =’歌曲名’ # 加表头,给A1单元格赋值 sheet[‘B1′] =’所属专辑’ # 加表头,给B1单元格赋值 sheet[‘C1′] =’播放时长’ # 加表头,给C1单元格赋值 sheet[‘D1′] =’播放链接’ # 加表头,给D1单元格赋值 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ for x in range(5): params = { ‘ct’: ’24’, ‘qqmusic_ver’: ‘1298’, ‘new_json’: ‘1’, ‘remote…

Read More

python 获取QQ音乐周杰伦前5页歌曲的歌词

# 直接运行代码就好 from bs4 import BeautifulSoup import requests # 引用requests模块 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ headers = { ‘origin’:’https://y.qq.com’, # 请求来源,本案例中其实是不需要加这个参数的,只是为了演示 ‘referer’:’https://y.qq.com/n/yqq/song/004Z8Ihr0JIu5s.html’, # 请求来源,携带的信息比“origin”更丰富,本案例中其实是不需要加这个参数的,只是为了演示 ‘user-agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like …

Read More

Python 抓取QQ音乐歌手所有音乐(以周杰伦为例)

# 直接运行代码就好 import requests # 引用requests模块 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ headers = { ‘origin’:’https://y.qq.com’, # 请求来源,本案例中其实是不需要加这个参数的,只是为了演示 ‘referer’:’https://y.qq.com/n/yqq/song/004Z8Ihr0JIu5s.html’, # 请求来源,携带的信息比“origin”更丰富,本案例中其实是不需要加这个参数的,只是为了演示 ‘user-agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Saf…

Read More

Python 最简单的抓取单个网页并把数据写入记事本

import requests #发出请求,并把返回的结果放在变量res中 res = requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md’) #打印状态码 200代表成功 print(res.status_code) #把Reponse对象的内容以字符串的形式返回 data=res.text #打开文件 没有则会自动新建abc.txt txt = open(‘abc.txt’,’w’) txt.write(data) txt.close() #关闭文件

Python +gevent 多线程抓取网站数据

from gevent import monkey #从gevent库里导入monkey模块。 monkey.patch_all() #monkey.patch_all()能把程序变成协作式运行,就是可以帮助程序实现异步。 import gevent,time,requests #导入gevent、time、requests。 start = time.time() #记录程序开始时间。 url_list = [‘https://www.baidu.com/’, ‘https://www.sina.com.cn/’, ‘http://www.sohu.com/’, ‘https://www.qq.com/’, ‘https://www.163.com/’, ‘http://www.iqiyi.com/’, ‘https://www.tmall.com/’, ‘http://www.ifen…

Read More

Python 自动抓取天气预报并且每天自动发送天气到指定邮箱

import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email.mime.text import MIMEText from email.header import Header # account = input(‘请输入你的邮箱:’) # password = input(‘请输入你的密码:’) # receiver = input(‘请输入收件人的邮箱:’) account = ‘121942198@qq.com’ password = ” #填写QQ密码,非登录密码,详见QQ邮箱设置里 receiver = ‘76123708@qq.com’ def weather_spider(): headers={‘user-agent’:’Mozilla…

Read More