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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python BeautifulSoup获取 网页链接的文字内容相关的知识,希望对你有一定的参考价值。


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


#!/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 link  in pagesoup.find_all(name=‘a‘,attrs={"href":re.compile(r‘^http:‘)}):
    print link.get_text()


本文出自 “linux与网络那些事” 博客,请务必保留此出处http://khaozi.blog.51cto.com/952782/1793077

以上是关于python BeautifulSoup获取 网页链接的文字内容的主要内容,如果未能解决你的问题,请参考以下文章

网页刮刮Python BeautifulSoup

使用 Python 和 BeautifulSoup(将网页源代码保存到本地文件中)

Python利用BeautifulSoup4库获取input标签的value值

Python 3.6 Beautiful Soup - 在网页抓取期间无法获取嵌入式视频 URL

Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息

[技术博客] BeautifulSoup4分析网页