码迷,mamicode.com
首页 > 编程语言 > 详细

获取当前页面的所有链接的三种方法对比(python 爬虫)

时间:2015-12-14 06:46:28      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:

 

‘‘‘
得到当前页面所有连接
‘‘‘

import requests

import re from bs4 import BeautifulSoup from lxml import etree
url
= ‘http://www.ok226.com‘ r = requests.get(url) r.encoding = ‘gb2312‘ # 利用 re (太黄太暴力!) matchs = re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\‘).+?(?=\‘)" , r.text) for link in matchs: print(link) print()

# 利用 BeautifulSoup4 (DOM树) soup = BeautifulSoup(r.text,‘lxml‘) for a in soup.find_all(‘a‘): link = a[‘href‘] print(link) print()

# 利用 lxml.etree (XPath) tree = etree.HTML(r.text) for link in tree.xpath("//@href"): print(link)

 

获取当前页面的所有链接的三种方法对比(python 爬虫)

标签:

原文地址:http://www.cnblogs.com/hhh5460/p/5044038.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!