python 实战爬取猿圈

news/2024/7/19 11:17:53 标签: python, 爬虫
python">import requests
import json
from bs4 import BeautifulSoup

#学习猿地 园圈

'''
分析爬取的数据
数据源地址 https://www.lmonkey.com/t
数据内容  
    文章标题 文章链接 作者 发布时间
工具 python


'''

url = 'https://www.lmonkey.com/t'
headers = {
    'User-Agent':'Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 72.0.3626.81 Safari / 537.36 SE 2.X MetaSr 1.0'
}
res = requests.get(url= url,headers = headers)

if res.status_code == 200 :
    print("请求发送成功")
    #print(res.text)
    #解析数据
    soup = BeautifulSoup(res.text,'lxml')
    #获取页面中所有的文章
    divs = soup.find_all('div',class_="list-group-item list-group-item-action p-06")

    varlist = []
    for i in divs:
        r = i.find('div', class_="topic_title")
        if r:
            print(i.span['title'])
            vardict ={'title':r.text.split('\n')[0],
                      'url':i.a['href'],
                      'author':i.strong.a.text,
                      'time':i.span['title']
                      }
            varlist.append(vardict)
    print(varlist)

    #存储数据
with open('./yuanquan.json','w',encoding='utf-8') as fp:
    # fp.write(varlist)
    json.dump(varlist,fp,ensure_ascii=False,sort_keys=True, indent=4)

with open('./yuanquan.json','r',encoding='utf-8') as f:
    print(f.read())

http://www.niftyadmin.cn/n/1269245.html

相关文章

python 爬虫登录

import requests from lxml import etree#封装类 进行学习园地的登录和订单的获取class LMonKey():#登录请求地址loginurl https://www.lmonkey.com/login#账户中心地址orderurl https://www.lmonkey.com/my/order#请求headerheaders {User-Agent:Mozilla / 5.0(Windows NT …

python 分页爬取数据

#分页数据爬取 import time import requests from lxml import etree from fake_useragent import UserAgent#请求页面程序 def getPage(url):#使用指定url,返回请求的页面headers {User-Agent:Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML,…

python 爬取豆瓣电影

import requests from lxml import etree import re from bs4 import BeautifulSoup import os import time import json #解析数据 def parse(res):res_html etree.HTML(res)items res_html.xpath(//div[class"item"])datalist []#遍历封装数据,并返回…

python 爬取代理IP网站

#爬取代理IP数据 import requests from lxml import etree# url https://www.xicidaili.com/nn/url http://ip.yqie.com/proxygaoni/headers {User-Agent:Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 72.0.3626.81 Safari / 5…

requests的基本使用

xpath-xml路径语言.py #xpath专门在xml中查找信息的语言 使用时加上 from lxml import etree from lxml import etree #方法一 text <!DOCTYPE html> <html> <head> <meta charset"utf-8"/> <title>我的学习</title> </he…

机器学习算法分类python实现

对葡萄酒数据集进行测试&#xff0c;由于数据集是多分类且数据的样本分布不平衡&#xff0c;所以直接对数据测试&#xff0c;效果不理想。所以使用SMOTE过采样对数据进行处理&#xff0c;对数据去重&#xff0c;去空&#xff0c;处理后数据达到均衡&#xff0c;然后进行测试&am…

pygame 小游戏

随着小球的运动&#xff0c;背景颜色也在不断改变&#xff0c;效果图如下&#xff1a; 代码如下&#xff1a; import pygame,syspygame.init() vInfo pygame.display.Info() print(vInfo) #size width,height vInfo.current_w,vInfo.current_h size width,height 600,4…

342. 4的幂 python

342. 4的幂 给定一个整数&#xff0c;写一个函数来判断它是否是 4 的幂次方。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 整数 n 是 4 的幂次方需满足&#xff1a;存在整数 x 使得 n 4x 示例 1&#xff1a; 输入&#xff1a;n 16 输出&…