当前位置:首页 > Python教程 > python技巧

杂记 使用Python采集网页内容

BeautifulSoup  一个分析、处理DOM树的类库。

 

采集所有img标签的title属性的内容

            #
             -*- coding: utf-8 -*- 
            from    urllib.request    import    urlopen
from    urllib.error    import    HTTPError
from    bs4    import    BeautifulSoup

url  = "http://qa.beloved999.com/category/view?id=2"
url  = "http://beloved.finley.com/category/view?id=24"
html = urlopen(url)
bs   = BeautifulSoup(html.read(),"html.parser")
res  = bs.findAll("img", "item-image")
print(len(res))
for a in res:
    print(a[title])
    

发送的HTTP_USER_AGENT是Python-urllib/3.4。包含HTTP的信息有

‘HTTP_ACCEPT_ENCODING‘ => ‘identity‘
‘HTTP_CONNECTION‘ => ‘close‘
‘HTTP_HOST‘ => ‘beloved.finley.com‘
‘HTTP_USER_AGENT‘ => ‘Python-urllib/3.4‘  。

原文:http://www.cnblogs.com/mafeifan/p/4655782.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐