# requests.post to get cookies print('\n','*'*50,'POST for login','*'*50) body = {"userName":"admin","password":123456} r = requests.post('http://localhost:31547/login',data=body) print("PostResponse.status = ",r.status_code) print('PostResponse.txt = ',r.text) print('PostResponse.cookies',r.cookies) # cookies cks = r.cookies
# requests.get with cookies print('\n','*'*50,'GET all users with cookies','*'*50) r = requests.get('http://localhost:31547/user',cookies=cks) print(r.text)
print('\n','*'*50,'GET user info with cookies','*'*50) r = requests.get('http://localhost:31547/user/1',cookies=cks) print(r.text)
# requests.get articles print('\n','*'*50,'GET all articles with cookies','*'*50) r = requests.get('http://localhost:31547/article',cookies=cks) print(r.text)
print('\n','*'*50,'GET article info','*'*50) r = requests.get('http://localhost:31547/article/4',cookies=cks) print(r.text)
# requests.post for add a article print('\n','*'*50,'Add article','*'*50) # body body = { "id": 39, "title": "机器学习Machine Learning", "content": "机器学习与人工智能", "authorId": "1"
html = ''' <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <c href="http://example.com/elsie" class="sister" id="link1"><!--This is a comment--></c>, <p class="story">...</p> '''
print('\n\n','*'*60,'Iterate the DOM','*'*60,'\n') # contents cont = soup.body.contents print(type(cont),'len(cont)=',len(cont)) for i in cont: print('>>>',i)
# children chils = soup.body.children print(type(chils),chils) for i in chils: print("*"*20,i)
for i in a.previous_elements: print('全部前后节点:', i.name)
2.6搜索文档树 : find_all( name , attrs , recursive , text , **kwargs )
2.6.1 name参数 1.字符串, 2.正则表达式, 3.列表, 4.True, 5.函数
1 2 3 4 5 6 7 8 9
for i in soup.find_all(['a','b','c']): print('搜索所有文档: ', i) for i in soup.find_all(re.compile('^b')): print('搜索文档with正则: ', i.name)
defhas_class_but_no_id(tag): return tag.has_attr('class') andnot tag.has_attr('id') for i in soup.find_all(has_class_but_no_id): print('搜索文档with函数: ', i.name)
2.6.2 keyword参数
1 2 3 4 5 6
for i in soup.find_all(id='link1'): print('keyword参数id : ',i)
#class是python的关键词, 因此使用class_ for i in soup.find_all(class_='sister'): print('keyword参数class_ : ', i)
2.6.3 text参数
1 2 3 4
for i in soup.find_all(text=['Elsie','Lacie']): print('text参数 : ', i) for i in soup.find_all(text=re.compile('Dormouse')): print('text参数 : ',i)
2.6.4 limit参数
1 2
for i in soup.find_all('a',limit=2): print('limit参数 : ',i)