221rrere 发表于 2016-6-27 09:19:02

python BeautifulSoup获取 网页链接的文字内容

这里和 获取链接略有不同,不是得到链接到url,而是获取每个链接的文字内容


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/opt/yrd_soft/bin/python

import re
import urllib2
import requests
import lxml
from bs4 import BeautifulSoup

url = 'http://www.baidu.com'

#page=urllib2.urlopen(url)
page=requests.get(url).text
pagesoup=BeautifulSoup(page,'lxml')
for linkin pagesoup.find_all(name='a',attrs={"href":re.compile(r'^http:')}):
    print link.get_text()



页: [1]
查看完整版本: python BeautifulSoup获取 网页链接的文字内容