美文网首页
用python写爬虫--3.2解析网页+beautifulsou

用python写爬虫--3.2解析网页+beautifulsou

作者: ddm2014 | 来源:发表于2018-04-02 11:05 被阅读0次

    beautifulsoup和pyquery一样,也是解析网页用的,还是同样的例子。
    逻辑还是一样,先找大项,再找你想要的小项。

    import requests
    from pyquery import  PyQuery as pq
    from bs4 import BeautifulSoup
    
    def bs():
        head = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
        re = requests.get('https://faxian.smzdm.com/', headers=head)
        file = BeautifulSoup(re.text, 'lxml')
        for item in file.select('.feed-block-ver'):
            print(item.select('.feed-ver-title')[0].text)
            print(item.select('.z-ellipsis')[0].text)
            print(item.select('.feed-ver-title a')[0].get('href'))
    

    逻辑是一样,只是用法上稍有不同。
    file.select('.feed-block-ver')返回一个list所以是可迭代的,所以在找小项时要把list拆解开,即用list[0],返回第一项,就不用像pyquery一样还要专门写.eq(),来找第几项。
    用text属性提取其文字信息。
    提取标签信息用get方法,看起来就是一样,不过最近觉得还是pyquery更简洁。

    相关文章

      网友评论

          本文标题:用python写爬虫--3.2解析网页+beautifulsou

          本文链接:https://www.haomeiwen.com/subject/qquthftx.html