# @Time : 2020/3/12 12:34 # @Author : felix # @Email : [email protected] # @File : handlerandom.py # @Software: PyCharm import random from faker import Factory from faker import Faker import radar import datetime import uuid import time import time fake = Factory().create() fake_cn = Faker("zh_CN") def generate_uuid(): return uuid.uuid1() def date_now(): return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') def date_now_sleep_stamp(): # print(str(datetime_stamp2[1:10])) time.sleep(0.1) t = time.time()
import random from faker import Factory fake = Factory().create('zh_CN') def random_name(): """随机中文姓名""" return fake.name() def random_name_en(): """随机英文字母姓名""" full_name = "atuo_" + fake.first_romanized_name() return full_name def random_phone_number(): """随机手机号""" return fake.phone_number() def random_email(): """随机email""" return fake.email() def random_str(min_chars, max_chars): """随机可控长度字符串""" return fake.pystr(min_chars=min_chars, max_chars=max_chars)
# 导入faker模块 from faker import Factory fake = Factory().create(locale="zh_CN") # print(dir(fake)) for i in range(1, 11): print("第{0}次随机产生的数据:".format(i)) print("用户姓名:{0}".format(fake.name())) print("用户名:{0}".format(fake.user_name())) print("密码:{0}".format(fake.password())) print("电子信箱:{0}".format(fake.email())) print("电话号码:{0}".format(fake.phone_number())) print("住址:{0}".format(fake.address())) print("所在城市:{0}".format(fake.city())) print("段落:{0}".format(fake.paragraph())) print("文本:{0}".format(fake.text())) print("IPv4:{0}".format(fake.ipv4())) print("网址:{0}".format(fake.url())) print("指定长度的字符串:{0}".format(fake.pystr(min_chars=6, max_chars=12)))
import re import logging import random import requests import pymysql from faker import Factory from bs4 import BeautifulSoup start_url = 'http://www.fang.com/SoufunFamily.htm' headers = {'User-Agent': Factory().create().user_agent()} mysql_setting = { 'host': '127.0.0.1', 'port': 3306, 'user': '******', 'password': '******', 'charset': 'utf8', 'db': 'spider' } conn = pymysql.connect(**mysql_setting) cursor = conn.cursor() execute = cursor.execute sql = 'select ip, port, types from proxies limit 500' execute(sql) proxies = cursor.fetchall() def write_to_mysql(city='null', title='null', detail='null', price='null'): sql = 'insert into soufang(city, title, detail, price) values ("{}", "{}", "{}", "{}")'.format( city, title, detail, price)
def test_eval_returns_valid_response_100(): fake = Factory().create() x = fake.mac_address() assert mac_audit.evaluate_mac(x, x) == 100
import unittest from apps.main import db from apps.auth.models import User from faker import Factory from random import randrange faker = Factory().create() users_number = randrange(1, 1000) class TestUser(unittest.TestCase): def shortDescription(self): return "[[ Generate %s users ]]\n" % (users_number, ) def test_password_setter(self): for _ in range(users_number): u = User(password=faker.password()) self.assertTrue(u.password_hash) def test_confirmation(self): for _ in range(users_number): u = User(password=faker.password()) self.assertFalse(u.confirmed) class CreateUsers(unittest.TestCase): def shortDescription(self): return "[[ Create %s users ]]" % (users_number) def test_create_users(self): for _ in range(users_number):
import random import threading import pymysql import requests from faker import Factory from bs4 import BeautifulSoup # 必须附带cookie,没有cookie返回的数据为重复数据 HEADERS = { 'Cookie': '_T_WM=5d1bd420e8679cba48cfbe5d39e885cd; WEIBOCN_FROM=1110006030; MLOGIN=0; ' 'M_WEIBOCN_PARAMS=luicode%3D10000011%26lfid%3D102803_ctg1_4288_-_ctg1_4288%26fid%3D102803_ctg1_4288_' '-_ctg1_4288%26uicode%3D10000011', 'User-Agent': Factory().create().user_agent(), } MYSQL = { 'host': '127.0.0.1', 'port': 3306, 'user': '******', 'password': '******', 'db': 'spider', 'charset': 'utf8' } # 入口链接 START_STR = """新闻 https://m.weibo.cn/api/container/getIndex?containerid=102803&openApp=0&page= 搞笑
def name(): """Fake name generator.""" fake = Factory().create('en_US') while True: yield (fake.name(), fake.address(),)
def get_data_from(url, date): global dct_all, form_data num = 0 dct_each_day = {} headers = { 'User-Agent': Factory().create().user_agent(), 'Referer': 'https://booking.airasia.com/', 'Cookie': 'LanguageSelect=cn/zh; true_loc=cn; X-CDN-Geo-Country=CN; X-CDN-Geo-City=SHANGHAI; _gcl_au=1.1.3512672.1539829882; currencySelect=CNY; __airasiaga=GA1.2.1508029182.1539829884; __airasiaga_gid=GA1.2.873579685.1539829884; acw_tc=65e300cc15398301995174164ea2cae773b3c677a90de1ad8bfefbbeb3; ASP.NET_SessionId=3baxaazmk1u1xkgsz1bqjc03; appID=W001; __RequestVerificationToken=ujANo7banTdpdPmhbLua4CPC-u-WtSxcV3uYerYz7IQhxaknfTi1koEqMcMsdXT85TPZBjtszVbTI47Tl7UZzSUhN_F56xjF8L5kGELkeGMJlK3gtdMa0gfjImvabp7Uh5HRmw2; dotrez=1343415306.20480.0000; rxVisitor=1539830193977I0TJLG6B2E67DKU5QTNL4UBQQG18CRT5; displaySSR=%7B%22meal%22:%22%22,%22departureDate%22:%222019-1-18%22,%22bookingDate%22:%222018-10-18%22%7D; flightSelect=PEK|DPS|2019-01-18|N|1|0|0|O|N; displayFare=%7B%22displayFare%22%3A1463.59%2C%22resultsLink%22%3A%22https%3A//booking.airasia.com/Flight/Select%3Fo1%3DPEK%26d1%3DDPS%26culture%3Dzh-CN%26dd1%3D2019-01-18%26ADT%3D1%26s%3Dtrue%26mon%3Dtrue%26cc%3DCNY%26c%3Dfalse%22%2C%22currency%22%3A%22CNY%22%2C%22destinationCity%22%3A%22%25E5%25B7%25B4%25E5%258E%2598%25E5%25B2%259B%2520%28DPS%29%22%2C%22departureCity%22%3A%22%25E5%258C%2597%25E4%25BA%25AC%2520%28PEK%29%22%7D; nearest_airports={"city":"KYOTO","airports":"KIX|NGO|HND"}; jaceToken=; dotRezSignature=WVlaRUhnUkNEb1k9bXphYXhhYjN8MzBjanFiMXpkRjBmVkkvOFdtelU2T3gxeXpuSEtZU2hzb1M1Qlg0bzVDcGNzZ2t4MXUxa2I2S3l3a2VuODkycStDRDk5MDAyQkNGTXREZ0c5Tm9ZMnNmcEVkMDlNcWpKTnY0NTFjTWdOcWM3K3Q5UzNoK1FGWnp1dExJU2hDc2MrOTYySnpNNHcvbEhQOEJuS2lQTC9DLzBvUVpvSStGNVRHND0=; userSession=cc=en-GB&mcc=MYR&rc=WWWA&ad=3baxaazmk1u1xkgsz1bqjc03&p=&st=1539831432.60542; dtPC=5$231425785_933h-vDLFUKHINQOOHOJLJQLOILVOLKLHGNJLK; rxvt=1539833295154|1539830193980; dtLatC=2; lastOrigin=PEK; lastDestination=SYD; lastDepartureDate=17/01/2019; lastReturnDate=19/01/2019; dtCookie=5$2F6E248DAE7078FBA3E8BDE2E263F03A|booking.airasia.com|1; dtSa=true%7CC%7C-1%7CSearch%7C-%7C1539831498211%7C231425785_933%7Chttps%3A%2F%2Fbooking.airasia.com%2F%7CAirAsia%20%5Ep%20Booking%20%5Ep%20Book%20low%20fares%20online%20-%20Search%7C1539831495156%7C' } response = requests.get(url, headers=headers, verify=False, proxies=get_proxy()[-1], data=form_data) response.encoding = 'utf-8' html = response.text soup = BeautifulSoup(html, 'lxml') items = soup.select('table.avail-table-info') for item in items: lst = [] for i in range(2): try: # 起飞地 depAirport = item.select('tr.fare-dark-row')[i].select( 'div.text-center > div')[1].text # 起飞时间 depTime = item.select('tr.fare-dark-row')[i].select( 'div.avail-table-bold')[0].text # 落地城市 arrAirport = item.select('tr.fare-dark-row')[i].select( 'div.text-center > div')[3].text # 落地时间 arrTime = item.select('tr.fare-dark-row')[i].select( 'div.avail-table-bold')[1].text # 航班型号 flight_info = item.select( 'div.carrier-hover-bold')[i].text.split() carrier = flight_info[0] # 航班编号 flightNumber = flight_info[1] except: depAirport = item.select('tr.fare-light-row')[i].select( 'div.text-center > div')[1].text depTime = item.select('tr.fare-light-row')[i].select( 'div.avail-table-bold')[0].text arrAirport = item.select('tr.fare-light-row')[i].select( 'div.text-center > div')[3].text arrTime = item.select('tr.fare-light-row')[i].select( 'div.avail-table-bold')[1].text flight_info = item.select( 'div.carrier-hover-bold')[i].text.split() carrier = flight_info[0] flightNumber = flight_info[1] depAirport = ''.join(re.findall('\w', depAirport)) arrAirport = ''.join(re.findall('\w', arrAirport)) dct = { 'depAirport%d' % i: depAirport + ' 从{}起飞'.format(depAirport), 'depTime%d' % i: depTime + ' 起飞时间', 'arrAirport%d' % i: arrAirport + ' 落地城市为{}'.format(arrAirport), 'arrTime%d' % i: arrTime + ' 落地时间', 'carrier%d' % i: carrier + ' 航班型号', 'flightNumber%d' % i: flightNumber + ' 航班编号', } lst.append(dct) dct_each_day[num] = lst num += 1 dct_all[date] = dct_each_day
def __init__(self): self.fake = Factory().create('zh_CN')
# The download delay setting will honor only one of: #CONCURRENT_REQUESTS_PER_DOMAIN = 16 #CONCURRENT_REQUESTS_PER_IP = 16 # Disable cookies (enabled by default) #COOKIES_ENABLED = False # Disable Telnet Console (enabled by default) #TELNETCONSOLE_ENABLED = False # Override the default request headers: DEFAULT_REQUEST_HEADERS = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', 'User-Agent': Factory().create().user_agent() } # Enable or disable spider middlewares # See https://doc.scrapy.org/en/latest/topics/spider-middleware.html #SPIDER_MIDDLEWARES = { # 'proxies.middlewares.ProxiesSpiderMiddleware': 543, #} # Enable or disable downloader middlewares # See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html #DOWNLOADER_MIDDLEWARES = { # 'proxies.middlewares.ProxiesDownloaderMiddleware': 543, #} # Enable or disable extensions
def __init__(self, language): self.fake = Factory().create(language)
import random from faker import Factory fake = Factory().create('en_us') def random_first_name(): return fake.first_name() def random_last_name(): return fake.last_name() def random_email(): return fake.email() def random_project_name(min_chars=6, max_chars=10): return fake.pystr(min_chars=min_chars, max_chars=max_chars)
import factory from faker import Factory from django.test import TestCase from django.contrib.auth import get_user_model from core.tests.factory import factories User = get_user_model() faker = Factory() class ModelTests(TestCase): def setUp(self): self.user = factories.UserFactory() def test_create_user_with_username_successful(self): """test creating a new user with an username successful""" username = "******" password = "******" user = factories.UserFactory(username=username, password=password) self.assertEqual(user.username, username) self.assertTrue(user.check_password(password)) def test_make_user_superuser(self): """Test that makes user a superuser""" self.user.make_user_a_superuser() self.assertTrue(self.user.is_superuser) def test_make_user_expuser(self): """Test that makes user a expert user""" res = self.user.make_user_an_expertuser()
#-*- coding:utf-8 -*- """一些生成器方法,生成随机数,手机号,以及连续数字等""" import os import sys base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(base_dir) from utils.config import DATA_PATH import random from faker import Factory fake = Factory().create('en_US') def random_phone_number(): return fake.phone_number() def random_name(): return fake.name() def random_address(): return fake.address() def random_email(): return fake.email() def random_ipv4(): return fake.ipv4() def random_str(min_chars=0, max_chars=8):