Home > Backend Development > Python Tutorial > Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Release: 2023-07-25 14:40:08
forward
754 people have browsed it

1. Crawling Bingbing’s animated pictures

The first step is very simple. You can directly search for Bingbing’s photos by going to a certain degree.

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Right-click on the picture you like, open the picture in a new tab, and copy the url above, as shown in the picture:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Use requests.get(url).content to get the image, and use with open to save it locally.

I selected 3 Bingbing GIFs and saved them in a list and randomly returned one. It is easy for you to expand the Bingbing library. The code is as follows.

def getbb():
    w0='https://img1.baidu.com/it/u=1762637264,598758602&fm=26&fmt=auto&gp=0.jpg'
    w1='https://img1.baidu.com/it/u=2231058723,1803013600&fm=11&fmt=auto&gp=0.jpg'
    w2='https://img0.baidu.com/it/u=3960011140,3634140813&fm=11&fmt=auto&gp=0.jpg'
    wlist=[w0,w1,w2]
    global i
    i=random.randint(0,2)#随机选取一个冰冰动图
    url=wlist[i]
    req=requests.get(url).content
    with open(f'wbb{i}.gif','wb') as p:
        p.write(req)
Copy after login

2. Automatically generate confession words

The confession words here are what you want to say to Bingbing If so, you can type it yourself. Of course, if you are too lazy to speak your own words, the editor will also automatically crawl literary poems on the Internet for you. The more poet-like you are, the more elegant you will be.

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

This function is also encapsulated into a function as follows:

def getwords():
    texts = []
    url = 'https://www.duanwenxue.com/huayu/lizhi/list_{}.html'.format(random.randint(1,114))
    response = requests.get(url)
    texts.append(response.text)
    articles = []
    for text in texts:
        soup = BeautifulSoup(text,'lxml')
        arttis = soup.find('div', class_='list-short-article').find_all('a', {'target': "_blank"})  # 寻找情话内容
            #  通过列表推导式以及for循环获取到每个a标签里面的text内容并通过strip去除空格
        articles.extend([arttis[i].text.strip() for i in range(len(arttis))])
    todaywords = articles[random.randint(0, len(articles)-1)]   # 随机选取其中一条情话
    return todaywords
Copy after login

3. Bingbing’s reply ing

The next step is to get to the point. You have spoken to Bingbing, and you still want Bingbing to reply to you. How do you do this?

Here, an intelligent robot is used to simulate Bingbing and generate reply content.

There are many intelligent chat robots now. It is recommended that if you have money, you can choose Turing robot, and if you are free, you can use Qingyunke.

This article is implemented using Qingyunke. No registration is required, just call the interface directly, which is very convenient.

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Encapsulate it into a function, input confession words, and automatically return intelligent dialogue:

def qingyunke(msg):
    url = f'http://api.qingyunke.com/api.php?key=free&appid=0&msg={msg}'
    html = requests.get(url)
    return html.json()["content"]
Copy after login

Readers who are interested can try this Function, if you use the sao words crawled in the second step as the input msg of the function, a very interesting thing will happen:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

4. Bingbing sent you a new message Email

The last step is to ask Bingbing to send you a private message and attach her beautiful photos~

This can be done using a common email address, such as 163 email or QQ email.

Take 163 mailbox as an example, click Settings:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Click POP3/SMTP/IMAP:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Click to start the IMAP/SMTP service:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

An authorization code will be generated. Copy this authorization code and use it later.

The following is the code I wrote. You only need to fill in your account number and authorization code.

def sendemail():
    msgword = getwords()
    res = qingyunke(msgword)
    xhx='你的163邮箱账号'#你实际使用的163邮箱账号
    pwd = '你的授权密码'#刚刚生成的163授权密码
    wy163list=[xhx]#收件人列表,可以扩充
    host_server = 'smtp.163.com'  #163邮箱smtp服务器
    sender = f'{xhx}@163.com' #发件人邮箱
    receiver = f'{wy163list[0]}@163.com'#收件人
    mail_title = '冰冰向您发送了新邮件' #邮件标题
    #邮件正文内容
    #为保证接口稳定,调用频率请控制在200次/10分钟
    mail_content = f"亲爱的{wy163list[0]},我是冰冰,<p>上次收到你给我的来信:<p>{msgword}<p>我很感动,特意给你回信并附上冰冰的美照哦~<p>现在我想对你说:<p>{res}<p>您好,<p>欢迎关注我的CSDN个人账号以获取最新创意好文,<p>开启python魔法之旅:</p> <p><a href=&#39;https://blog.csdn.net/x978404178?spm=1001.2100.3001.5343&#39;>点击此处进入CSDN</a></p>"
    msg = MIMEMultipart()
    #将图片显示在正文
    global i
    with open(f&#39;wbb{i}.gif&#39;, &#39;rb&#39;) as f:
        #图片添加到正文
        msgImage = MIMEImage(f.read())
        # 定义图片ID
    msgImage.add_header(&#39;Content-ID&#39;, &#39;<image1>&#39;)
    msg.attach(msgImage)
    msg["Subject"] = Header(mail_title,&#39;utf-8&#39;)
    msg["From"] =Header("冰冰","utf-8")
    msg["To"] = receiver
    msg.attach(MIMEText(mail_content,&#39;html&#39;))
    try:
        smtp = SMTP_SSL(host_server) # ssl登录连接到邮件服务器
        smtp.set_debuglevel(1) # 0是关闭,1是开启debug
        smtp.ehlo(host_server) # 跟服务器打招呼,告诉它我们准备连接,最好加上这行代码
        smtp.login(sender,pwd)
        smtp.sendmail(sender,receiver,msg.as_string())
        smtp.quit()
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("无法发送邮件")
Copy after login

五、本文完整代码

到这里呢,本文就该告一段落了,小编这里把整体代码都奉上啦,欢迎大家动手实践,有问题可以随时私我噢~

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import random
import requests
from smtplib import SMTP_SSL
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.image import MIMEImage
 
def getbb():
    w0=&#39;https://img1.baidu.com/it/u=1762637264,598758602&fm=26&fmt=auto&gp=0.jpg&#39;
    w1=&#39;https://img1.baidu.com/it/u=2231058723,1803013600&fm=11&fmt=auto&gp=0.jpg&#39;
    w2=&#39;https://img0.baidu.com/it/u=3960011140,3634140813&fm=11&fmt=auto&gp=0.jpg&#39;
    wlist=[w0,w1,w2]
    global i
    i=random.randint(0,2)#随机选取一个冰冰动图
    url=wlist[i]
    req=requests.get(url).content
    with open(f&#39;wbb{i}.gif&#39;,&#39;wb&#39;) as p:
        p.write(req)
 
def getwords():
    texts = []
    url = &#39;https://www.duanwenxue.com/huayu/lizhi/list_{}.html&#39;.format(random.randint(1,114))
    response = requests.get(url)
    texts.append(response.text)
    articles = []
    for text in texts:
        soup = BeautifulSoup(text,&#39;lxml&#39;)
        arttis = soup.find(&#39;div&#39;, class_=&#39;list-short-article&#39;).find_all(&#39;a&#39;, {&#39;target&#39;: "_blank"})  # 寻找情话内容
            #  通过列表推导式以及for循环获取到每个a标签里面的text内容并通过strip去除空格
        articles.extend([arttis[i].text.strip() for i in range(len(arttis))])
    todaywords = articles[random.randint(0, len(articles)-1)]   # 随机选取其中一条情话
    return todaywords
 
def qingyunke(msg):
    url = f&#39;http://api.qingyunke.com/api.php?key=free&appid=0&msg={msg}&#39;
    html = requests.get(url)
    return html.json()["content"]
 
def sendemail():
    msgword = getwords()
    res = qingyunke(msgword)
    xhx=&#39;你的163邮箱账号&#39;#你实际使用的163邮箱账号
    pwd = &#39;你的授权密码&#39;#刚刚生成的163授权密码
    wy163list=[xhx]#收件人列表,可以扩充
    host_server = &#39;smtp.163.com&#39;  #163邮箱smtp服务器
    sender = f&#39;{xhx}@163.com&#39; #发件人邮箱
    receiver = f&#39;{wy163list[0]}@163.com&#39;#收件人
    mail_title = &#39;冰冰向您发送了新邮件&#39; #邮件标题
    #邮件正文内容
    #为保证接口稳定,调用频率请控制在200次/10分钟
    mail_content = f"亲爱的{wy163list[0]},我是冰冰,<p>上次收到你给我的来信:<p>{msgword}<p>我很感动,特意给你回信并附上冰冰的美照哦~<p>现在我想对你说:<p>{res}<p>您好,<p>欢迎关注我的CSDN个人账号以获取最新创意好文,<p>开启python魔法之旅:</p> <p><a href=&#39;https://blog.csdn.net/x978404178?spm=1001.2100.3001.5343&#39;>点击此处进入CSDN</a></p>"
    msg = MIMEMultipart()
    #将图片显示在正文
    global i
    with open(f&#39;wbb{i}.gif&#39;, &#39;rb&#39;) as f:
        #图片添加到正文
        msgImage = MIMEImage(f.read())
        # 定义图片ID
    msgImage.add_header(&#39;Content-ID&#39;, &#39;<image1>&#39;)
    msg.attach(msgImage)
    msg["Subject"] = Header(mail_title,&#39;utf-8&#39;)
    msg["From"] =Header("冰冰","utf-8")
    msg["To"] = receiver
    msg.attach(MIMEText(mail_content,&#39;html&#39;))
    try:
        smtp = SMTP_SSL(host_server) # ssl登录连接到邮件服务器
        smtp.set_debuglevel(1) # 0是关闭,1是开启debug
        smtp.ehlo(host_server) # 跟服务器打招呼,告诉它我们准备连接,最好加上这行代码
        smtp.login(sender,pwd)
        smtp.sendmail(sender,receiver,msg.as_string())
        smtp.quit()
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("无法发送邮件")
 
if __name__ == '__main__':
    getbb()
    sendemail()
Copy after login

运行时间大概在30s~1min30s哦,快登录你的邮箱查看冰冰给你的悄悄话吧。

效果如下:

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached)

好了,各位有没有get到冰冰呢?欢迎在下方评论区留言讨论哦。

六、总结

    本文基于Python网络爬虫,抓取了王冰冰靓女的动图图片,之后利用网络爬虫技术获取了文学诗篇网站的表白桥段,通过青云客平台,打造了一款智能机器人模拟冰冰回信,并且基于邮箱服务器,模拟冰冰向自己发送新邮件,每天打开邮箱,都可以收到女神的邮箱,心情美滋滋~如此有趣的项目,快快来尝试吧!

The above is the detailed content of Teach you step by step how to use Python web crawler + automation to create your own virtual female ticket (source code attached). For more information, please follow other related articles on the PHP Chinese website!

source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template