Python教程

python之requests

本文主要是介绍python之requests,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

'''
&name : xiaowen
&time : 2021/8/19
&content : 获取段子
'''

import requests
from lxml import etree
import re
import time

def get_content(url):
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/7.36 (KHTML, like Gecko) Chrome/3.0.3.61 Safari/7.36"}
resbost = requests.get(url = url,headers = headers)
resbost.encoding = 'utf-8'
html_1 = etree.HTML(resbost.text)
z_div = html_1.xpath('/html/body/div[1]/div/div[2]/div')
#print(z_div)
for i in z_div:
s = i.xpath('./a[1]/@href')[0]
#print(s)
url_1='https://www.
.com'+s
resbost_1 = requests.get(url = url_1,headers = headers)
resbost_1.encoding = 'utf-8'
html_2 = resbost_1.text
s_1 = re.compile(r'<di* class="content">(?P.?)</di>',re.S)
text_content = s_1.finditer(html_2)
resbost_1.close()
for j in text_content:
time.sleep(1)
print('\n',j.group("content"))
if name == 'main':
print('欢迎使用本程序

这篇关于python之requests的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!