2015-11-23 47 views
0

我無法讓我的程序正常工作,而且我嘗試了這麼久。在這裏,它非常簡單,但我無法得到它。應該返回任何包含「html」的東西。這真的令人沮喪。這是一個命令行的Python 2.x的返回多個「href」

#!/usr/bin/env python 

import sys 
import re 

#Make this program work both on python 2.x and Python 3.x 
if (sys.version_info[0] == 3): raw_input = input 

import urllib2 
url = urllib2.urlopen('http://makeitwork.com/') 
data = url.read() 
urlsearch = re.findall(r'href=[\'"]?([^\'"]+)' , data) 

for x in urlsearch: 
    line = x.split() 
    print(" %s" %line[0]) 
+0

尋求調試幫助的問題(**「爲什麼不是這個代碼工作?」**)必須包含所需的行爲,*特定的問題或錯誤*和*必要的最短代碼*來重現它**自問**。沒有**明確問題陳述**的問題對其他讀者沒有用處。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – MattDMo

回答

3

嘗試BeautifulSoupNever use regex to parse HTML code

import urllib2 
from bs4 import BeautifulSoup 

url = urllib2.urlopen('http://makeitwork.com/') 
data = url.read() 

soup = BeautifulSoup(data) 
for i in soup.find_all(a): 
    print(link.get('href')) 
0

嘗試使用這個表達式

'r'a\shref="/?(.*)">' 

<a href HTML標記之後和之前基本上尋找什麼>閉幕聲明。