python爬虫(爬取段子)

python爬取段子

爬取某个网页的段子

第一步

无论三七二十一咱们先导入模块python

#http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc  段子所在的网址
import re 
import  requests   #若是没这模块运行CMD pip  install requests

第二步

获取网站的内容编辑器

#http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc  段子所在的网址
import re 
import  requests   #若是没这模块运行CMD pip  install requests
 
response = requests.get(http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc)
data = response.text

第三步

找到段子所在的位置ide

#http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc  段子所在的网址
import re 
import  requests   #若是没这模块运行CMD pip  install requests
 
response = requests.get('http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc')   #这个编辑器的长度关系无法放一行
data = response.text
#按F12选择本身想要的内容所在的位置copy出来
new_list = re.findall('<span class="bjh-p">(.*?)</span></p><p>',data ) # (.*?)是咱们要的内容

第四部

保存文件网站

#http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc  段子所在的网址
import re 
import  requests   #若是没这模块运行CMD pip  install requests
 
response = requests.get('http://baijiahao.baidu.com/s?id=1598724756013298998&wfr=spider&for=pc')   #这个编辑器的长度关系无法放一行
data = response.text
#按F12选择本身想要的内容所在的位置copy出来
new_list = re.findall('<span class="bjh-p">(.*?)</span></p><p>',data ) # (.*?)是咱们要的内容

for a in new_list:
    with open(r'D:\图片\段子.txt', 'a') as fw:
        fw.write(a)
        fw.flush()
相关文章
相关标签/搜索