最新消息:

Python学习笔记,Python常用函数学习

Python学习 追逐 199浏览 0评论

Python学习笔记,Python常用函数学习

Python函数

交互命令行 help(函数名) 查看帮助信息

abs() 绝对值函数

int() 转换为整型

cmp() 比较函数,需要2个参数 大于为1 等于为0 小于为-1

str() 转换为字符串

自定义函数

def 函数名():

函数返回值 return()

递归函数

可变参数 args

 

夜景20190418

夜景20190418

import requests
from lxml import etree

def lj(url):
    #url='https://www.zcool.com.cn/?p=2'
    html=requests.get(url).text
#print(html)
    nr=etree.HTML(html)
    a=nr.xpath('//*[@id="body"]/main/div[3]/div/div/div[2]/p[1]/a')
#//*[@id="body"]/main/div[3]/div/div[1]/div[2]/p[1]/a
#//*[@id="body"]/main/div[3]/div/div[4]/div[2]/p[1]/a
    #print(a[0].text)
    title=[]
    link=[]
    sj=[]
    lk=''
    for x in a:
        title.append(x.text)
        link.append(x.attrib['href'])
        lk=lk+x.attrib['href']+'\r\n'
        sj.append(str(x.text)+'|'+str(x.attrib['href']))
        #print(title)
        #print(link)
    with open(r'C:\Users\Administrator\Desktop\link.txt','w',encoding='utf-8') as f:
        f.write(lk)
    #return(title)
    return(sj)


#print(lj('https://www.zcool.com.cn/?p=2'))
def jlsj(n):
    ss=''
    for i in range(1,n):
        url=f'https://www.zcool.com.cn/?p={i}'
        ss=ss+str(lj(url))
        with open(r'C:\Users\Administrator\Desktop\cs.txt','w',encoding='utf-8') as f:
            f.write(ss)

jlsj(2)

 

# -*- coding=utf-8 -*-
import requests
import re

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}

keyword=(input('请输入关键词:',))
url='https://www.baidu.com/s?wd='+keyword
html=requests.get(url,headers=headers).text
#print(html)
ze=r'<div id="rs"><div class="tt">相关搜索</div><table cellpadding="0">(.+?)</table></div>'
xgss=re.findall(ze,html,re.S)
#print(xgss)
xgze=r'<th><a href="(.+?)">(.+?)</a></th>'
sj=re.findall(xgze,str(xgss),re.S)
#print(sj)
gjc=''
for x in sj:
    print(x[1])
    gjc=gjc+x[1]+'\r\n'

print(gjc)
with open(r'C:\Users\Administrator\Desktop\gjc.txt', 'w', encoding='utf-8') as f:
    f.write(gjc)

转载请注明:二爷记 » Python学习笔记,Python常用函数学习

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址