V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
qazwsxkevin
V2EX  ›  Python

(爬虫)是否有现成的轮子,对页面的文字+连接直接提取输出?

  •  
  •   qazwsxkevin · 2019-06-20 01:03:26 +08:00 · 2656 次点击
    这是一个创建于 1743 天前的主题,其中的信息可能已经有所发展或是发生改变。

    要爬的页面三天两头变样子,原来是 lxml+etree+xpath 定位元素,提取 herf,herf 的 text() 现在变化的情况,也许这种方式不好维护,
    请问各位高手,有没有现成的轮子或者思路,对整个页面的超链接进行提取成[('urltext_1',url1),('urltext_2',url2)],类似这样的?
    也许对 urltext 做个匹配合适的目标就好了,忽然觉得做这事情,没必要把事情复杂化。。。

    17 条回复    2019-06-20 17:52:53 +08:00
    siknet
        1
    siknet  
       2019-06-20 01:06:27 +08:00 via Android
    你都说了三天两头变。。。不是一小时一变就应该知足了
    nicevar
        2
    nicevar  
       2019-06-20 07:33:49 +08:00
    现成的轮子一大堆,你去 github 上搜一下就行了, 但是现成的轮子做的都比较复杂,你光学会用的时间都能用 python 的 scrapy 库写一个满足你这种简单需求的了
    jorneyr
        3
    jorneyr  
       2019-06-20 07:54:37 +08:00
    Jsou 解析 html 很方便,可以使用选择器,一次提取出所有的 a 标签。
    XxxxD
        4
    XxxxD  
       2019-06-20 09:23:14 +08:00
    光提取超链接和文字的话,用 re 也可以啊,但是你不好判断哪些是你需要的,哪些是不需要的
    cominghome
        5
    cominghome  
       2019-06-20 09:44:02 +08:00
    爬虫是持久战,看是他们前端先倒下还是爬虫工程师先倒下,在这之前,斗争都将持续
    zarte
        6
    zarte  
       2019-06-20 09:46:23 +08:00
    xpth 直接获取 a 标签?
    shuizhengqi
        7
    shuizhengqi  
       2019-06-20 09:47:47 +08:00
    还不如有没有现成的工具能直接帮你完成功能,然后数据落库
    lixuda
        8
    lixuda  
       2019-06-20 09:56:04 +08:00
    @jorneyr python 也有这样库?
    Northxw
        9
    Northxw  
       2019-06-20 10:03:44 +08:00
    re
    Threeinchtime
        10
    Threeinchtime  
       2019-06-20 10:19:15 +08:00
    diffbot
    了解一下,不便宜
    ben1024
        11
    ben1024  
       2019-06-20 10:23:06 +08:00
    用 PHP 写了一个

    links.php
    ```
    <?php
    require __DIR__ . '/vendor/autoload.php';

    global $base_uri, $wait_replace_imgs;
    $base_uri = 'https://www.v2ex.com';
    $t1 = microtime(true);
    try {
    set_time_limit(1800);
    ini_set("max_execution_time", 1800);
    ini_set('memory_limit', '512M');
    $html_node = file_get_contents($base_uri);
    $crawler = new \Symfony\Component\DomCrawler\Crawler($html_node, $base_uri);
    $links = $crawler->filter('a')->links();
    foreach ($links as $link) {
    $temp_links[] = ['url' => $link->getUri(), 'text' => $link->getNode()->textContent];
    }
    file_put_contents('links.txt', json_encode($temp_links, JSON_UNESCAPED_UNICODE));
    echo 'success<br/>';
    $t2 = microtime(true);
    echo 'time consuming ' . round($t2 - $t1, 3) . ' s' . PHP_EOL;
    } catch (Exception $exception) {
    echo $exception->getCode() . ', message:' . $exception->getMessage();
    }
    ```
    部分效果
    ```
    [
    {
    "url": "https:\/\/www.v2ex.com\/t\/575511",
    "text": "写代码的时候没有思路 不知道如何写起,请教如何培养训练编程思路 谢谢!"
    },
    {
    "url": "https:\/\/www.v2ex.com\/member\/fanmouji",
    "text": ""
    },
    {
    "url": "https:\/\/www.v2ex.com\/t\/575397",
    "text": "JD 的 618 是不是走走过场?"
    }
    ]
    ```
    项目地址
    https://github.com/MasterCloner/Cornerstone
    shawndev
        12
    shawndev  
       2019-06-20 10:23:51 +08:00
    Distill Web Monitor
    kppwp
        13
    kppwp  
       2019-06-20 10:28:48 +08:00 via iPhone
    @lixuda 支持这种功能的选择器应该是最低要求。
    leopku
        14
    leopku  
       2019-06-20 10:33:56 +08:00
    推荐一下 https://github.com/MontFerret/ferret
    自写代码部分是 DSL,很简单的几行就行,改起来自然更简单更快

    框架本身是 go 的,运行效率没得说
    Takamine
        15
    Takamine  
       2019-06-20 11:17:05 +08:00
    度一下,keywords: 爬虫 采集工具。
    tikazyq
        16
    tikazyq  
       2019-06-20 11:36:30 +08:00
    可以试试 Crawlab 的自动提取字段功能,成功率大概在 50-70%

    https://github.com/tikazyq/crawlab

    文章: https://juejin.im/post/5cf4a7fa5188254c5879facd
    dsg001
        17
    dsg001  
       2019-06-20 17:52:53 +08:00
    xpath 没写好吧,难道每天换主题?否则大改 css 怎么解决?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3246 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 14:01 · PVG 22:01 · LAX 07:01 · JFK 10:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.