吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2586|回复: 11
收起左侧

[Python 转载] Python爬取学习猿地文章标题,链接,时间,作者

[复制链接]
luoshiyong123 发表于 2020-5-31 15:33
[Python] 纯文本查看 复制代码
'''
爬取网址:学习猿地猿圈
爬取内容:文章标题,文章连接,作者,时间
工具:bs4,requests
结果:爬取到文件之中
'''
from bs4 import BeautifulSoup
import requests,json
#1.定义请求头和url
url = 'https://www.lmonkey.com/t'
headers = {
     "User-Agent":	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36"
}
alldata = []
#2.请求获取数据
res = requests.get(url = url,headers = headers)
if res.status_code == 200:

#3.解析数据
    soup = BeautifulSoup(res.text,'lxml')
#获取页面中所有的文章
    divclass = soup.find_all( 'div',class_="list-group-item list-group-item-action p-06")
    for i in divclass:
        my_title = i.find('div',class_="topic_title mb-0 lh-180")
        if my_title:
            my_title = my_title.text.split("\n")[0]
            my_url = i.a["href"]
            my_time = i.span['title']
            my_author = i.strong.a.text
            print(my_author)
            print(my_url)
            print(my_time)
            print(my_title)
            temp = {'title':my_title,
                    'passage_url':my_url,
                    'time':my_time,
                    'author':my_author
                    }
            alldata.append(temp)
            #4.写入文件
    with open('C:/Users/lsy/Desktop/lsy.json','w') as fp:
        json.dump(alldata,fp)

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
funtouch2 + 1 + 1 我很赞同!
zgy150010318 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

随遇而安8 发表于 2020-5-31 15:42
稳坐沙发,学习,支持一下
FJ19950531 发表于 2020-5-31 16:40
ciker_li 发表于 2020-6-1 10:16
ircer 发表于 2020-6-1 11:07
不错,代码很干净整洁
hshcompass 发表于 2020-6-14 19:05
圆满下载,谢谢分享。
xy0225 发表于 2020-7-21 11:32
学习一下,支持
rainleaf 发表于 2020-7-21 11:53
不明觉厉。 看着舒服。
whatwho 发表于 2020-7-21 15:49
坐下学习爬取思路
acr05s 发表于 2020-7-21 15:54
python代码就是这么清爽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-10 02:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表