homework 06

import requests
import re
import multiprocessing

def PUT(q):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
    }
    url = 'https://www.baidu.com/s?wd=%E7%95%99%E4%B8%8B%E9%82%AE%E7%AE%B1'
    response = requests.get(url,headers=headers)
    html = response.text
    #print(html)
    res = re.compile('href="(.*?)"')
    resx = res.findall(html)
    http_list = []
    for http in resx:
        if "http" in http:
            http_list.append(http)
    for url_ in http_list:
        #print(url_)
        q.put(url_)

def GET(q):
    while True:
        url_ = q.get()
        #print(url_)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
        }
        responses = requests.get(url_, headers=headers)
        htmls = responses.text
        #print(htmls)   #正常
        e = re.compile("[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?")
        email = e.findall(htmls)
        #print(email)    
        b = 0
        #li = ['baidu.com','163.com','qq.com','162.com','sina.com','139.com','123.com','nen.com']
        if len(email) != 0:
            for i in email:
                if '@baidu.com' in i or '@163.com' in i or '@qq.com' in i or '@162.com' in i or '@sina.com' in i or '@139.com' in i or '@123.com' in i or '@139.com' in i or '@nen.com' in i :
                    b += 1
                    print(i)
                    if b<5000:
                        with open('E:/python/Homework05/novel/email.txt',mode='a',encoding='utf8') as f: 
                            f.write(i+'\n')
                    else:
                        break  

if __name__ == "__main__":
        q = multiprocessing.Queue()
        p = multiprocessing.Process(target = PUT,args = (q,))
        p1 = multiprocessing.Process(target = GET,args = (q,))
        p.start()
        p1.start()
        p.join()
        p1.join()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值