1. python读取用户名密码
user = ['xrz','wyh']
password = ['123456','654321']
size = len(user)
n = 0
while True :
setting = input("请问您需要登陆,注册,退出?1.登陆,2.注册,3.退出\n")
if setting == '1':
User = input("请输入您的用户名:\n")
while n <= size:
if User == user[n]:
Password = input("请输入您的用户密码:\n")
if Password == password[n]:
print("欢迎",user[n])
break
else:
print("密码错误")
break
n = n + 1
if n == size:
print("用户名不存在!")
break
elif setting == '2':
user.append(input("请输入你要注册的用户名:\n"))
password.append(input("请输入你的密码:\n"))
print("注册成功!")
elif setting == '3':
break
else:
print("请输入正确的需求!")
2. python爬虫项目实战:爬取用户的所有信息,如性别、年龄等
python爬虫项目实战:
爬取糗事网络用户的所有信息,包括用户名、性别、年龄、内容等等。
10个步骤实现项目功能,下面开始实例讲解:
1.导入模块
import re
import urllib.request
from bs4 import BeautifulSoup
2.添加头文件,防止爬取过程被拒绝链接
def qiuShi(url,page):
################### 模拟成高仿度浏览器的行为 ##############
heads ={
'Connection':'keep-alive',
'Accept-Language':'zh-CN,zh;q=0.9',
'Accept':'text/html,application/xhtml+xml,application/xml;
q=0.9,image/webp,image/apng, / ;q=0.8',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
headall = []
for key,value in heads.items():
items = (key,value)
headall.append(items)
opener = urllib.request.build_opener()
opener.addheaders = headall
urllib.request.install_opener(opener)
data = opener.open(url).read().decode()
################## end ########################################
3.创建soup解析器对象
soup = BeautifulSoup(data,'lxml')
x = 0
4.开始使用BeautifulSoup4解析器提取用户名信息
############### 获取用户名 ########################
name = []
unames = soup.find_all('h2')
for uname in unames:
name.append(uname.get_text())
#################end#############################
5.提取发表的内容信息
############## 发表的内容 #########################
cont = []
data4 = soup.find_all('div',class_='content')
data4 = str(data4)
soup3 = BeautifulSoup(data4,'lxml')
contents = soup3.find_all('span')
for content in contents:
cont.append(content.get_text())
##############end####################################
6.提取搞笑指数
#################搞笑指数##########################
happy = []
data2 = soup.find_all('span',class_="stats-vote")
data2 = str(data2) # 将列表转换成字符串形式才可以使用
soup1 = BeautifulSoup(data2,'lxml')
happynumbers = soup1.find_all('i',class_="number")
for happynumber in happynumbers:
happy.append(happynumber.get_text())
##################end#############################
7.提取评论数
############## 评论数 ############################
comm = []
data3 = soup.find_all('a',class_='qiushi_comments')
data3 = str(data3)
soup2 = BeautifulSoup(data3,'lxml')
comments = soup2.find_all('i',class_="number")
for comment in comments:
comm.append(comment.get_text())
############end#####################################
8.使用正则表达式提取性别和年龄
######## 获取性别和年龄 ##########################
pattern1 = '<div class="articleGender (w ?)Icon">(d ?)</div>'
sexages = re.compile(pattern1).findall(data)
9.设置用户所有信息输出的格局设置
################## 批量输出用户的所以个人信息 #################
print()
for sexage in sexages:
sa = sexage
print(' ' 17, '= = 第', page, '页-第', str(x+1) + '个用户 = = ',' ' 17)
print('【用户名】:',name[x],end='')
print('【性别】:',sa[0],' 【年龄】:',sa[1])
print('【内容】:',cont[x])
print('【搞笑指数】:',happy[x],' 【评论数】:',comm[x])
print(' ' 25,' 三八分割线 ',' ' 25)
x += 1
###################end##########################
10.设置循环遍历爬取13页的用户信息
for i in range(1,14):
url = ' https://www.qiushike.com/8hr/page/'+str(i)+'/'
qiuShi(url,i)
运行结果,部分截图:
3. 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=%s&order_by=%s'
cookies={
'UM_distinctid': '15b89d53a930-02ab95f11ccae2-51462d15-1aeaa0-15b89d53a9489b',
'CNZZDATA1258679142': '1544557204-1492664886-%7C1493280769',
'_session_id': '%3D%3D--',
'remember_user_token':'Q3LjYwODEwNzgiXQ%3D%3D--',
'_ga': 'GA1.2.2016948485.1492666105',
'_gid': 'GA1.2.382495.1494550475',
'Hm_lpvt_': '1494550475',
'Hm_lvt_': '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=Q3LjYwODEwNzgiXQ%3D%3D--; _ga=GA1.2.2016948485.1492666105; _gid=GA1.2.824702661.1494486429; _gat=1; Hm_lvt_=1494213432,1494213612,1494321303,1494387194; Hm_lpvt_=1494486429; _session_id=%3D%3D--',
'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':'www.jianshu.com',
"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 = "www.jianshu.com" + 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
4. Python设计一个用户名和密码
name=input()
if name=='这个地方是你判断用的用户名':
缩进pass=input()
缩进if pass=='这里是你判断的密码':
缩进缩进print('用户名密码都对')
缩进else:
缩进缩进print('密码不对')
else:
缩进print('用户名不对')
5. python 获取登录名getpass.getuser 怎么处理root
getpass 模块提供了平台无关的在命令行下输入密码的方法. getpass(prompt) 会显示提示字符串, 关闭键盘的屏幕反馈, 然后读取密码. 如果提示参数省略, 那么它将打印出 "Password:". getuser() 获得当前用户名, 如果可能的话.
6. python作业:1.设计账号登录程序,要求如下:(1)动态获取用户名和密码,格式为"用户名:密码"
这个需求没说清楚用什么平台,比如是桌面版,还是web版。对所用技术或者库有没有什么限制。