使用Python进行网页抓取

发布于 2021-02-02 23:14:31

我想从网站上获取每天的日出/日落时间。是否可以使用Python抓取网络内容?使用什么模块?有没有可用的教程?

关注者
0
被浏览
89
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    结合使用urllib2和出色的BeautifulSoup库:

    import urllib2
    from BeautifulSoup import BeautifulSoup
    # or if you're using BeautifulSoup4:
    # from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(urllib2.urlopen('http://example.com').read())
    
    for row in soup('table', {'class': 'spad'})[0].tbody('tr'):
        tds = row('td')
        print tds[0].string, tds[1].string
        # will print date and sunrise
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看