一尘不染

使用python和BeautifulSoup从网页检索链接

python

如何检索网页链接并使用Python复制链接的URL地址?


阅读 354

收藏
2020-02-11

共1个答案

一尘不染

这是在BeautifulSoup中使用SoupStrainer类的一小段代码:

import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')

for link in BeautifulSoup(response, parse_only=SoupStrainer('a')):
    if link.has_attr('href'):
        print(link['href'])
2020-02-11