推荐学习书目
› 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
kiko12324
V2EX  ›  Python

新人学 Python 爬虫,用了 BS4,目前只会爬取具体的 url,求问如何爬取整个页面呢或者是某个日期呢

  •  
  •   kiko12324 · Apr 19, 2022 · 3840 views
    This topic created in 1621 days ago, the information mentioned may be changed or developed.
    爬取的网站: https://www.beiei.com/navisample.php
    爬取目标:公司名称

    如图:这是爬取单个页面,用 request 请求下来再用 bs4 解析设置条件为 li 和 li0 就能够获取了,但是在外面的 url 好像都是没有啥关联的...


    如图:比如爬取 4.18 号,发现用 bs4 不知道该怎么取到该段 HTML ,怎么用 bs4 限制到这段呢
    6 replies  •  2022-04-25 15:55:55 +08:00
    janda
        1
    janda  
       Apr 19, 2022
    xpath
    colatea
        2
    colatea  
       Apr 19, 2022
    我用 xpath,大同小异,取到 div 内容为 2022.04.18 以后,向上找父节点,再向下找 table
    html.xpath("\\div[text()='2022.04.18']/../table/tbody")
    Ritter
        3
    Ritter  
       Apr 19, 2022
    百度 bs4 doc
    NotFoundEgg
        4
    NotFoundEgg  
       Apr 19, 2022
    divs = soup.findAll(name='div', attrs={"class": "dateDiv"})
    for div in divs:
    if '2022.04.18' in div.next:
    table = div.find_next('table')
    Joshuam
        5
    Joshuam  
       Apr 19, 2022 via Android
    推荐个 chrome 插件:SelectorGadget
    只需点点点你要的数据,他给你 CSS Selector ,然后使用 bs4 处理 CSS Selector
    AmberJiang
        6
    AmberJiang  
       Apr 25, 2022
    建议查看 BS4 的官方文档学习
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2771 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 13:22 · PVG 21:22 · LAX 06:22 · JFK 09:22
    ♥ Do have faith in what you're doing.