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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1194|回复: 31
收起左侧

[Python 原创] Python获取blog文章内容

[复制链接]
pengGgxp 发表于 2023-11-16 00:45
临时写的一个,应用场景佷有限,大家凭自己再扩展吧,我是因为要把一个文章迁移,强制复制也不行,就写了个这玩意,直接上图了:
QQ截图20231116004258.png
微信截图_20231116004319.png
这个可以再扩展成更自动的,可是我懒,希望有闲的没事的大佬扩展一下,我要借鉴(抄)~~

免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
hb100326 + 1 + 1 我很赞同!
syszsw + 1 + 1 谢谢@Thanks!
Bean2012 + 1 + 1 我很赞同!
echoaku + 1 + 1 谢谢@Thanks!

查看全部评分

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

 楼主| pengGgxp 发表于 2023-11-16 11:33
云在天 发表于 2023-11-16 06:34
为什么要提供图片, 不提供代码呢

代码在这
[Python] 纯文本查看 复制代码
import re
import requests
from lxml import etree

post_url = input('请输入文章地址:')
#根据文章地址get数据
res = requests.get(post_url)

xx = res.content.decode('utf-8')

x = etree.HTML(xx)
#需要获取父级xpath
#xpath示例://*[@id="article-container"]
#不会的百度吧
xpath = input('请输入xpath路径,可打开控制台查看:')
content = x.xpath(xpath + '//*')

ree = re.compile(r'class=".*"|id=".*"')
urll = re.compile(r'(?<=(src="))(/).*?(?=("))')
with open('resualt.txt', 'w', encoding='utf-8') as file:
    tep1 = ''
    for i in content:
        tep = etree.tostring(i, encoding='utf-8').decode('utf-8').strip()
        tep = re.sub(ree, '', tep)
        strr = re.search(urll, tep)
        #如果图片是相对路径,就自动替换成绝对路径,(需要自己寻找修改路径地址)
        #后面不用管,只需要找到前面的路径就行  就像https://dreamtea.top
        #需要自己实测
        if strr is not None:
            strr = strr.group()
            tep = re.sub(urll, 'https://cdn.com' + '/' + strr, tep)
            # print(tep)
            strr = None
        if tep != tep1 and tep in tep1:
            #print(tep)
            continue
        file.write(tep)
        tep1 = tep
print('导出完成!')
USER7086 发表于 2023-11-16 08:56
import re
import requests
from lxml import etree
post_url = input('请输入文章地址: ')
#根提文章地址get数据
res = requests. get(post_url)
xx= res. content. decode('utf-8')
x = etree. HTML(xx)
#需要获取父级xpath
#xpath示例: //*[@id="article-container"]
#不会的百度吧
xpath = input('请输入xpath路径, 可打开控制台查看:')
content = x. xpath(xpath + '//*')
ree = re. compile(r'class=".*"|id=".*"')
url l = re. compile(r'(?<=(src="))(/).*?(?=("))')
with open('resualt. txt', 'w', encoding='utf-8') as file:
tep1 = ''
for i in content:
tep = etree. tostring(i, encoding='utf-8'). decode('utf-8'). strip()
tep = re. sub(ree, ", tep)
strr = re. search(urll, tep)
#如果图片是想对路径,就自动背换成绝对路径,《需要自己寻找修改路径地址》
#后面不用筒,只需要找到煎面的路径就行。就像&#160;https://dreamtea.top
#需要自己实测
if strr is not None:
strr r = strr. group()
tep = re.sub(urll, '&#160;https://cdn.con'+'/'+strr,tep)
# print(tep)
strr = None
if tep != tep1 and tep in tep1:
#print(tep)
continue
file. write(tep)
tep1 = tep
print('导出完成!')
 楼主| pengGgxp 发表于 2023-11-16 00:46
如果有不会获取xpath可以直接百度!
或者按我的方法,打开开发者工具,进入选择模式,选中父级(最外面那个)元素,然后 右键->复制->复制xpath(或者复制完整xpath,基本一样)
云在天 发表于 2023-11-16 06:34
为什么要提供图片, 不提供代码呢
goditorjoker 发表于 2023-11-16 07:40
最好加上uer-agent防止反爬,直接请求可能会失败
sai609 发表于 2023-11-16 07:53
为什么要提供图片, 不提供代码呢
winxpnt 发表于 2023-11-16 08:04
好东东,收藏,感谢分享
chenqc 发表于 2023-11-16 08:23
牛逼,谢谢
CHENYF8318 发表于 2023-11-16 08:33
为什么要提供图片, 不提供代码呢?
xiaotwins 发表于 2023-11-16 08:34
学习了 不错不错
zlxtqbdqq 发表于 2023-11-16 08:34
要是有代码就更好了,我们也想借鉴
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

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

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

GMT+8, 2024-5-4 14:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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