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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5008|回复: 17
收起左侧

[Python 转载] 爬虫第二篇 论坛搜软件

  [复制链接]
yx_robert 发表于 2018-11-16 02:01
本帖最后由 yx_robert 于 2018-11-16 02:08 编辑

练练手
顺便解决点实际问题

求助xpath
用的还不是很顺手
有写的很丑陋的地方
求大神指点

[Python] 纯文本查看 复制代码
#! /usr/bin/env python
# -*- coding: UTF-8 -*-

from lxml import etree
import requests
import sys

reload(sys)
sys.setdefaultencoding('gbk')


def gbk_2_utf(_str):
    return _str.decode('gbk').encode('UTF-8')


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
    'Referer': 'https://www.52pojie.cn/forum-16-1.html'
}


save_file = u'搜索结果.txt'


# url_soft = 'https://www.52pojie.cn/forum-16-1.html'
# url_code = 'https://www.52pojie.cn/forum-24-1.html'
# url_movie = 'https://www.52pojie.cn/forum-56-1.html'

# Windows
# [url=https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&filter=typeid&typeid=231]https://www.52pojie.cn/forum.php ... r=typeid&typeid=231[/url] #windows
# [url=https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&typeid=231&filter=typeid&typeid=231&page=1]https://www.52pojie.cn/forum.php ... d&typeid=231&page=1[/url]
# [url=https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&typeid=231&typeid=231&filter=typeid&page=13]https://www.52pojie.cn/forum.php ... lter=typeid&page=13[/url]

# 辅助软件
# [url=https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&filter=typeid&typeid=289]https://www.52pojie.cn/forum.php ... r=typeid&typeid=289[/url]
# [url=https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&typeid=289&filter=typeid&typeid=289&page=2]https://www.52pojie.cn/forum.php ... d&typeid=289&page=2[/url]

main_web = 'https://www.52pojie.cn/'
url = 'https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=16&typeid=231&filter=typeid&typeid=231&page=%d'
max_pag = 50
filter_str = 'amp;'
tar_str = u'百度'
# tar_str = ''


def main():
    with open(save_file, 'w') as f:
        for i in range(1, max_pag + 1):
            cur_url = url % i
            req = requests.get(cur_url, headers=headers)
            req.encoding = 'gbk'
            # print req.text
            root = etree.HTML(req.text)
            # res = root.xpath('//*[@href="javascript:;"]/@class')
            # result1 = html.xpath('//li[contains(@class,"aaa") and @name="fore"]/a/text()')
            name_list = root.xpath(
                '//a[contains(@href, "forum.php?") and @onclick="atarget(this)"]/text()')
            url_list = root.xpath(
                '//a[contains(@href, "forum.php?") and @onclick="atarget(this)"]/@href')

            if len(name_list) == len(url_list):
                for idx in range(0, len(url_list)):
                    if tar_str == '':
                        f.write(name_list[idx] + '\n')
                        f.write(main_web + url_list[idx] + '\n\n\n')
                    else:
                        if name_list[idx].find(tar_str) != -1:
                            f.write(name_list[idx] + '\n')
                            f.write(main_web + url_list[idx] + '\n\n\n')
            # break
    f.close()


if __name__ == "__main__":
    main()

搜索结果.txt

273.86 KB, 下载次数: 16, 下载积分: 吾爱币 -1 CB

100页搜索结果方便查找

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
lxwen + 1 + 1 谢谢@Thanks!

查看全部评分

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

 楼主| yx_robert 发表于 2019-11-3 21:40
雷晨 发表于 2019-11-3 19:06
楼主你好,请问可以出个爬http://mzsock.com/这个网站的教程吗?谢谢了

........教唆未成年 犯法啊.. 不敢
拉风丶 发表于 2018-11-16 02:24
alairlee 发表于 2018-11-16 02:42 来自手机
cube 发表于 2018-11-16 03:40 来自手机
一路xpath
kzx136 发表于 2018-11-16 07:27 来自手机
还在学习中,谢谢分享
xiaobaibaibai 发表于 2018-11-16 08:26
正好学习 谢谢分享~
空心烂木头 发表于 2018-11-16 08:37
我也想学习,可惜基础为0,不过现在我用采集器用的比较溜,,需求都能满足
Cameron·陽 发表于 2018-11-16 08:38
虽然我看不懂,但还是支持你一下!!
wang_qianxu 发表于 2018-11-16 08:44 来自手机
好高深呀,支持
1sina 发表于 2018-11-16 08:48

虽然不懂,但还是支持你一下!!
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-26 03:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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