site=urllib2.urlopen(r'http://www.rom.on.ca/en/join-us/jobs')
for i,j in enumerate(site):
if "http://www.ontario.ca" in j: #j is the line
print i+1 #i is the number start from 0 normally in the html code is 1 the first line so add +1
>>620
import urllib2
from BeautifulSoup import BeautifulSoup
url = "http://www.my_url.com"
data = urllib2.urlopen(url).read()
soup = BeautifulSoup.BeautifulSoup(data)
all_links = soup.find('a')
# you can look for specific link
为什么不简单地使用
enumerate()
:在您的代码问题上,这将逐字读取。如果不传递要读取的数据量。在
你可以:
^{pr2}$这部分不完全是一个答案,但我建议您使用BeautifulSoup。在
一般情况下,您需要Xpath来实现这些目的。 示例:http://www.w3schools.com/xpath/xpath_examples.asp
Python有一个漂亮的库
lxml
: http://lxml.de/xpathxslt.html相关问题 更多 >
编程相关推荐