本文目录一览:
- 1、python怎么在Linux实现创建用户?
- 2、python设计函数,实现会员注册,要求用户名长度不小于3,密码长度不小于6, 注册时两次输入密码必须相同
- 3、python注册程序编写?
- 4、python批量注册账号 数据库会崩溃吗
- 5、python怎么爬取简书用户名
python怎么在Linux实现创建用户?
题主你好,
我以创建user1-user10个用户为例, 代码为测试截图如下:
1. 系统中不存在user1-user10:
执行脚本:
可以看到脚本执行后, 显示成功添加用户数10个, 失败0个.并且/etc/shadow中相应的用户信息也有了.
-------------
我们删掉user0, user1和user2:
可以看到/etc/shadow中的信息已经没有了user0-user2的信息了,此时我们再执行上面的脚本:
可以看到输出显示, 成功创建3个,就是我们之前删掉的那三个. 还有7个创建失败的, 因为用户本身就存在所以创建失败了.
希望可以帮到题主, 欢迎追问.
python设计函数,实现会员注册,要求用户名长度不小于3,密码长度不小于6, 注册时两次输入密码必须相同
def log_in():
username=input("输入用户名(不小于3位)")
if len(username)=3:
password=input("密码(不小于六位)")
if len(password)=6:
pass_1=input("再次输入密码")
if password==pass_1:
print("注册成功")
else:
print("两次输入密码不一致")
log_in()
else:
print("密码长度不符合要求")
log_in()
else:
print("帐号长度不符合要求")
log_in()
log_in()
python注册程序编写?
import
re
username_pattern
=
re.compile(r'_.{2,29}')
password_pattern
=
re.compile(r'(?=.*_)(?=.*\d)(?=.*[A-Za-z]).{6,18}')
然后用相应的pattern,去match用户输入的字符串即可。
python批量注册账号 数据库会崩溃吗
不会的,一般不会同时运行太多账号,数据库可支撑力是很大的,不用担心这个问题,要批量注册所需要用的代码如下:
# -*- coding:utf-8 -*-
import random,urllib,urllib2
import re,time
x=input("请输入需要注册的数量:")
# x=raw_input() #转换成字符串的
def h(i,y):
user=str(random.randrange(10000000,99999999))
QQ=str(random.randrange(10001,999999999999))
pwd=str(random.randrange(100000,99999999))
url=""
data={"username":user,
"password":pwd,
"repassword":pwd,
"email":QQ+"@qq.com",
"qq":QQ,
"sex":"0",
"action":"newuser",
"submit":""}
data=urllib.urlencode(data)
req=urllib2.Request(url,data=data)
print data
# html=urllib2.urlopen(req).read()
# print(html)
html=urllib2.urlopen(req).read().decode('gbk')
# print(type(html))
reg=u'您已成功注册成为本站用户'
reg=re.compile(reg)
r=re.findall(reg,html)
if r!=[]:
print("注册成功,账号为%s,密码为%s,目前注册到第%s,还剩%s个"%(user,pwd,i+1,y-i-1))
f=open("c:\user.txt","a")
f.write("%s----%s----%s@qq.com----%s\n" %(user,pwd,QQ,QQ))
# f.write("qq----123456")
f.close()
for i in range(x):
h(i,x)
# 延时
time.sleep(2)
python怎么爬取简书用户名
初步的思路
今天在用Scrapy写代码的时候,对网页的结构也有了大致的分析,再加上之前罗罗攀的思路,初步我是通过专题入口
热门专题
image.png
image.png
专题管理员 (一般粉丝、文章、字数、收获喜欢、这几项数据都非常漂亮)
image.png
image.png
以上红框里的数据项就是我需要爬取的字段
但是以上的思路存在一点的问题:
存在一些简书用户并不是一些热门专题的管理员,但是其人气粉丝量也很高,这个思路可能无法将这些用户爬取下来
进阶的思路
热门专题
专题关注的人
专题关注的人的动态
推荐作者 粉丝信息
image.png
image.png
image.png
优点:
数据大而全,基本包含了99%的用户(个人猜测,不严谨)
缺点:
因为许多用户不止关注一个专题,而且其中包含了大量的新注册用户(数据很多为空),并且也有大量重复数据需要去重
代码部分:
jianshu.py 还在调试阶段,待更新...
# -*- coding: utf-8 -*-
import sys
import json
import requests
import scrapy
import re
from lxml import etree
from scrapy.http import Request
reload(sys)
sys.path.append('..')
sys.setdefaultencoding('utf-8')
class jianshu(scrapy.Spider):
name = 'jianshu'
# topic_category = ['city']
topic_category = ['recommend', 'hot', 'city']
base_url = 'lections?page=%sorder_by=%s'
cookies={
'UM_distinctid': '15b89d53a930-02ab95f11ccae2-51462d15-1aeaa0-15b89d53a9489b',
'CNZZDATA1258679142': '1544557204-1492664886-%7C1493280769',
'_session_id': 'Q3RteU9BeTA3UVh1bHp1d24ydmZJaGdkRDZJblE3SWg3dTlNR2J1WmJ5NS9HNlpOZVg4ZUk0TnNObE5wYXc3SjhYcU5WR0NKZ3RhcE9veFVDU2RNWkpqNE44MWxuVmtoR1ZDVXBFQ29Kc1kzZmd4SVNZakJyWVN4c1RFQXZNTFhmUUtxemVDVWlVU1l3VW92NFpTeEE2Q0ppUVN0QVFEMUpLZjFHdHViR21zZko2b1lFTW9DR08yNDh5Z0pvd0VJRzc4aFBqRnZYbGt6QXlmSzMxdU1QTVFwUVcxdUViaElqZzh2Y1RwcENtSWxWbW5PMUVGZ2UrZ2xVcm1NTlpMK2x2UTdOWlZjUVNPK1dCTERpMnd6U3ZxbXlROENML2VseTRHUTBqbFE1ZUlqN1FqazJJK0tsV1htdEt1bnl5MkhCbHNJTmh1ejFLTW9pYVcrVmx0bit1blNXV1VCQ3JNbHAvK1Z5T1ZvUk5IMVMzR1dUNHBlWFZBamcwYjQxSzBjZVRvMGRZSDRmV0xtTGZHekF1M3V6dGcwMHhpQ24zdmVKelV5eDRFSWZ4QT0tLW1uSXNLakp6SW54SUo0QU16a2dFSkE9PQ%3D%3D--0849c37208f8c573960d857029c7d6a15145c419',
'remember_user_token':'W1szNDgxMjU3XSwiJDJhJDEwJDlSS3VLcFFWMlZzNFJuOFFNS1JQR3UiLCIxNDk0MjEzNDQ3LjYwODEwNzgiXQ%3D%3D--9241542a4e44d55acaf8736a1d57dd0e96ad4e7a',
'_ga': 'GA1.2.2016948485.1492666105',
'_gid': 'GA1.2.382495.1494550475',
'Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068': '1494550475',
'Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068': '1494213432,1494213612,1494321303,1494387194'
}
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept - Language': 'zh - CN, zh;q = 0.8',
'Connection': 'close',
'Cookie': 'UM_distinctid=15b89d53a930-02ab95f11ccae2-51462d15-1aeaa0-15b89d53a9489b; CNZZDATA1258679142=1544557204-1492664886-%7C1493280769; remember_user_token=W1szNDgxMjU3XSwiJDJhJDEwJDlSS3VLcFFWMlZzNFJuOFFNS1JQR3UiLCIxNDk0MjEzNDQ3LjYwODEwNzgiXQ%3D%3D--9241542a4e44d55acaf8736a1d57dd0e96ad4e7a; _ga=GA1.2.2016948485.1492666105; _gid=GA1.2.824702661.1494486429; _gat=1; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1494213432,1494213612,1494321303,1494387194; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1494486429; _session_id=czl6dzVOeXdYaEplRVdndGxWWHQzdVBGTll6TVg5ZXFDTTI5cmN2RUsvS2Y2d3l6YlkrazZkZWdVcmZDSjFuM2tpMHpFVHRTcnRUVnAyeXhRSnU5UEdhaGMrNGgyMTRkeEJYOE9ydmZ4N1prN1NyekFibkQ5K0VrT3paUWE1bnlOdzJrRHRrM0Z2N3d3d3hCcFRhTWdWU0lLVGpWWjNRdjArZkx1V2J0bGJHRjZ1RVBvV25TYnBQZmhiYzNzOXE3VWNBc25YSS93WUdsTEJFSHVIck4wbVI5aWJrUXFaMkJYdW41WktJUDl6OVNqZ2k0NWpGL2dhSWx0S2FpNzhHcFZvNGdQY012QlducWgxNVhoUEN0dUpCeUI4bEd3OXhiMEE2WEplRmtaYlR6VTdlZXFsaFFZMU56M2xXcWwwbmlZeWhVb0dXKzhxdEtJaFZKaUxoZVpUZEZPSnBGWmF3anFJaFZpTU9Icm4wcllqUFhWSzFpYWF4bTZmSEZ1QXdwRWs3SHNEYmNZelA4VG5zK0wvR0MwZDdodlhZakZ6OWRVbUFmaE5JMTIwOD0tLXVyVEVSeVdOLy9Cak9nVG0zV0hueVE9PQ%3D%3D--ea401e8c501e7b749d593e1627dbaa88ab4befc2',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
'Host':'',
"X-Requested-With": 'XMLHttpRequest'
}
def get_total_page(self):
#获取专题总页数 包含3个字典的列表 [{"hot": xx}, {"recommend":xx}, {"city": xx}]
total_page_list = []
for order in self.topic_category:
order = order.decode('utf-8')
total_page = 100
dict = {}
for page in range(1, total_page):
url = self.base_url % (page, order)
html = requests.get(url, headers=self.headers).content
selector = etree.HTML(html)
#print html
try:
elements = selector.xpath('//*[@id="list-container"]/div[1]/div/h4/a/text()')[0]
if elements is not Exception:
continue
except Exception :
dict['total_page'] = page - 1
dict['category'] = order
break
total_page_list.append(dict)
return total_page_list
def get_topic_info(self):
#获取专题信息
topic_info_list = []
total_page_list = self.get_total_page()
base_url = self.base_url
for dict in total_page_list:
category = dict['category']
total_page = int(dict['total_page'])
for page in range(1, total_page + 1):
url = base_url % (page, category)
html = requests.get(url, headers=self.headers,cookies=self.cookies).content
selector = etree.HTML(html)
topic_href = selector.xpath('//*[@id="list-container"]')[0]
for href in topic_href:
dict = {}
topic_name = href.xpath('./div/h4/a/text()')[0]
topic_url = "" + href.xpath('./div/h4/a/@href')[0]
topic_img_url = href.xpath('./div/a/img/@src')[0]
img_num = topic_img_url.split("/")[5]
dict['topic_name'] = topic_name
dict['topic_url'] = topic_url
#
dict['img_num'] = img_num
topic_info_list.append(dict)
return topic_info_list
def get_topic_admin_info(self):
#获取管理员信息
topic_admin_info_list = []
topic_info_list = self.get_topic_info()
for d in topic_info_list:
img_num = str(d['img_num'])
base_url = "s/editors_and_subscribers" % img_num
base_url_response = requests.get(base_url, headers=self.headers, cookies=self.cookies)
json_data_base = json.loads(base_url_response.text.decode('utf-8'))
editors_total_pages = json_data_base['editors_total_pages']
for page in range(1, int(editors_total_pages) + 1):
if page == 1:
editors = json_data_base['editors']
for editor in editors:
dict = {}
dict['nickname'] = editor['nickname']
dict['slug'] = editor['slug']
topic_admin_info_list.append(dict)
else:
try:
url = "}/editors?page={}".format(img_num, page)
response = requests.get(url,headers=self.headers,cookies=self.cookies)
json_data = json.loads(response.text.decode('utf-8'))
editors = json_data['editors']
for editor in editors:
dict = {}
dict['nickname'] = editor['nickname']
dict['slug'] = editor['slug']
topic_admin_info_list.append(dict)
except Exception:
pass
return topic_admin_info_list
def get_followers_following_list(self):
# 获取管理员粉丝列表
followers_list = []
topic_admin_list = self.get_topic_admin_info()
followers_base_url = "s/%s/followers"
for dict in topic_admin_list:
url = followers_base_url % dict['slug']
headers = self.headers
headers['Referer'] = url
headers['DNT'] = '1'
response = requests.get(url, headers=headers, cookies=self.cookies).content
total_followers = re.fi