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
hongfeiyu
V2EX  ›  Python

请教一个 python 爬虫的问题

  •  
  •   hongfeiyu · 2016-01-01 22:19:19 +08:00 · 2138 次点击
    这是一个创建于 3045 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚开始学爬虫,各种都还不是太熟
    请问这种网页的数据应该如何爬
    https://legacy.bas.ac.uk/met/READER/surface/stationpt.html
    请指点一下

    5 条回复    2016-01-02 11:04:24 +08:00
    hongfeiyu
        1
    hongfeiyu  
    OP
       2016-01-01 22:40:38 +08:00
    这个网站是不是有反爬虫机制啊,源代码并不能爬下来
    lecher
        2
    lecher  
       2016-01-02 00:01:11 +08:00 via Android
    晒请求代码,我随手拿 curl 都可以请求成功。
    请求带上伪造的 header 参数。除非封 IP 否则爬虫是禁不掉的。
    hongfeiyu
        3
    hongfeiyu  
    OP
       2016-01-02 07:40:56 +08:00
    @lecher
    我已经自己写了一个
    #-*-coding:utf8-*-
    import requests
    import re

    #把网页名存进数组
    # <a href ="Deception.00.temperature.html">00Z </a> 00 06 12 18
    #按网页开始循环,每次新建一个同名的 txt ,
    # <tr><th><h5> 1962</h5><th><h5>是表头
    # <th><h5><font color = black> -8.8<i>是数据

    html = requests.get('https://legacy.bas.ac.uk/met/READER/surface/stationpt.html')
    #print html.text
    site = re.findall('<a href ="(.*?)">',html.text,re.S)
    for each in site:
    print '正在分析子站点',each
    f=open(each,'a')
    small=requests.get('https://legacy.bas.ac.uk/met/READER/surface/'+each)
    # data=re.findall('<th><h5><font color = black>(.*?)<i>',small.text,re.S)
    # for each in data:
    f.writelines(small.text)
    f.close()

    我发现直接把网页扒下来还好用一些
    因为表格可以直接复制进 execl
    可以更容易分析数据
    tonyrft
        4
    tonyrft  
       2016-01-02 09:19:55 +08:00
    解析 html 请使用 Beautifulsoup


    import requests
    from bs4 import BeautifulSoup


    response = requests.get('https://legacy.bas.ac.uk/met/READER/surface/stationpt.html')
    soup = BeautifulSoup(response.content, 'lxml')
    for subPage in soup.find_all('a'):
    html = requests.get(''.join(('https://legacy.bas.ac.uk/met/READER/surface/', subPage['href']))).text

    不算空格一共 6 行
    建议使用第三方库直接写入 excel
    lx9921
        5
    lx9921  
       2016-01-02 11:04:24 +08:00
    @hongfeiyu 和 2 楼说的一样,单纯的 request 恐怕不行,需要带上 header 参数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1297 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:14 · PVG 01:14 · LAX 10:14 · JFK 13:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.