python爬取网页每日一句英语发给微信好友

Posted starjuly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬取网页每日一句英语发给微信好友相关的知识,希望对你有一定的参考价值。

步骤

  • 链接:http://dict.eudic.net/home/dailysentence
  • 使用urllib获取网页代码
  • 使用etree解析网页数据并使用xpath找到想要的内容
  • 扫码登录微信
  • 输入微信好友名字(一定要名字一样)并匹配好友
  • 发送

效果如下

代码

# 获取每日一句英语
from lxml import etree
from urllib.request import urlopen
import urllib
import itchat as ic

## 地址
url = 'http://dict.eudic.net/home/dailysentence'
req = urllib.request.Request(url)
html = urlopen(req).read()
# 解析网页数据
html = etree.HTML(html)
# 获取想要的内容
result = html.xpath('.//div[@id="senten_move"]/p/text()')
# 英文句子
english = result[0]
# 翻译
chinese = result[1]
all = '每日一句:\\n' + english + '\\n' + chinese
# 登录微信
ic.auto_login()
# 输入微信好友名字
name = input()
# 查找微信好友
friends = ic.search_friends(name)
friendName = friends[0]['UserName']
# 发送消息
ic.send(all, friendName)

以上是关于python爬取网页每日一句英语发给微信好友的主要内容,如果未能解决你的问题,请参考以下文章

微信,爬取每日一句,发送至多人,多个群

微信,爬取每日一句,文本,schedule函数定时发送消息

Python爬取金山词霸每日一句,存储到MySQL中

python爬取所有微信好友的信息

每日一句名人金句学英语(20221127)

每日一句名人金句学英语(20221127)