beautifulsouppython
1. python中beautifulsoup简单使用
file = open(filename)
soup=Beautifulsoup(file,'lxml' )
aElement = soup.select('div#autInfo div.info_c a')
text1 = aElement.string
text2 = soup.select('div#autDescription div.info_c').string
我是按你图片的内容取的,select里面遵循CSS选择器语法,具体的CSS选择器语法你可以网络一下。
2. 如何在 python 中使用 beautifulsoup4 来抓取标签中的内容
小白入门阶段,主要用requests和beautifulsoup4库来爬取内容。目前遇到的问题是,使用beautifulsoup抓取标签内容出错。所以来咨询下过往前辈的建议。
1、像上图HTML文档中的滴滴出行,应该如何抓取?用select函数可以实现嘛?
2、像抓取战略投资,我使用了下面的语句,内容截取到了,但是还多了个括号。不知道怎么把括号去掉。
investment=soup.select('span[class="t-small c-green"]')[0].text.strip()
3、我光是select函数就用迷糊了。。。更别说添加别的函数了。
问题比较简单,但是已经卡了我很久了。求大神指点一二啊!
from bs4 import BeautifulSoup
html_doc = '''
<div class="line-title">
<span class="title">
<b>
滴滴出行
<span class="t-small c-green">
(战略投资)
</span>
</b>
</span>
<span class="sechovershow jzbtn c-lined small marl10 act-ugc-edit act-ugc-edit-base1" style="margin-top:-5px">
<i class="fa fa-pencil"></i>
编辑
</span>
</div>
'''
soup = BeautifulSoup(html_doc, "html.parser")
# 初级版
didi = soup.b.next_element.strip()
invest = soup.b.span.next_element.strip()
# 进阶版
didi, invest = soup.b.stripped_strings
3. python中的beautifulsoup和xpath有什么异同点
BeautifulSoup是一个库,而XPath是一种技术,python中最常用的XPath库是lxml,因此,这里就拿lxml来和BeautifulSoup做比较吧
1 性能 lxml >> BeautifulSoup
BeautifulSoup和lxml的原理不一样,BeautifulSoup是基于DOM的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多。而lxml只会局部遍历,另外lxml是用c写的,而BeautifulSoup是用python写的,因此性能方面自然会差很多。
2 易用性 BeautifulSoup >> lxml
BeautifulSoup用起来比较简单,API非常人性化,支持css选择器。lxml的XPath写起来麻烦,开发效率不如BeautifulSoup。
title = soup.select('.content div.title h3')
同样的代码用Xpath写起来会很麻烦
title = tree.xpath("//*[@class='content']/div[@class='content']/h3")
3 总结
需求比较确定,要求性能的场合用lxml,快速开发用BeautifulSoup
ps: BeautifulSoup4可以使用lxml作为parser了
4. Python beautifulsoup 获取标签中的值 怎么获取
age = soup.find(attrs={"class":"age"}) #你这里find只要一个attrs参数不会报错。
if age == None: #简单点可以用 if not age:
print u'没有找到'
else:
soup.find(attrs={"class":"name"})
#否则用findAll找出所有具有这个class的tr
tr = html.find("tr", attrs={"class":"show_name"})
tds = tr.findAll("td")
for td in tds:
print td.string # 或许不是string属性,你可以用dir(td)看看有哪些可用的。
(4)beautifulsouppython扩展阅读:
1、如果是函数定义中参数前的*表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中。
1)如定义以下函数:
def func(*args):print(args)
当用func(1,2,3)调用函数时,参数args就是元组(1,2,3)
2)如定义以下函数:
def func(**args):print(args)
当用func(a=1,b=2)调用函数时,参数args将会是字典{'a':1,'b':2}
学python的同时一定会接触到其他技术,毕竟光会python这门语言是不够的,要看用它来做什么。比如说用 python做爬虫,就必须接触到html, http等知识。
python是现在最火的数据分析工具语言python的进阶的路线是数据清洗,爬虫,数据容器,之后是卷积,线性分析,和机器学习,区块连,金融方面的量化等高端进阶。
5. python beautifulsoup应用问题
最好是先看一下 driver.page_source 的内容,确保里面有你先要的数据。
查一下 beautifulsoup 的文档,有关 css 选择器的部分,对一下 find_all 里面的语法是不是正确 。在写 find_all 的时候一层一层选下去,不要一开始就写到最里面一层。
平时写解析的时候用 lxml 比较多,beautifulsoup 的写法太久没写过了。
6. python怎么安装beautifulsoup
在cmd(命令行)中输入以下命令:
python -m pip install bs4从bs4中调用beautifulsoup:
from bs4 import BeautifulSoup
7. mac版python怎么安装beautifulsoup
一、安装python
sudo rm -rf /System/Library/Frameworks/Python.framework/
sudo rm -rf /Library/Frameworks/Python.framework/
sudo rm -rf /Applications/Python\ 2.7/
sudo rm -rf /usr/local/bin/*
重启机器
以上步骤是卸载已安装的python,第1个命令卸载系统自带的,第2个命令卸载用户安装的。
二、安装pydev(Eclipse中的python插件)
在pydev官网下载pydev的zip包,这比传统的使用site.xml的eclipse插件安装方式效率要高。
对于eclipse3.4以上版本,将zip包解压到eclipse/dropin即可。
三、安装beautifulsoup4
解压安装包,shell下先cd进入目录,然后python setup.py install。
四、安装lxml
lxml是强大的xml和html的python库。
五、安装crapy
首先安装setuptools, 选择与本机python版本一致的安装包,如setuptools-0.6c11-py2.7.egg (md5)
shell下sh setuptools-0.6c11-py2.7.egg。
setuptools安装后可使用easy_install命令,这是安装python相关包的命令。
8. python beautifulsoup可以做什么
Beautiful Soup是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。
它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。
它可以大大节省你的编程时间。
9. Python关于BeautifulSoup的用法
创建一个字符串,例子如下:
Python
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
创建 beautifulsoup 对象
Python
soup = BeautifulSoup(html)
另外,还可以用本地 HTML 文件来创建对象,例如
soup = BeautifulSoup(open('index.html'))
上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象
下面来打印一下 soup 对象的内容,格式化输出
print soup.prettify()
<html>
<head>
<title>
The Dormouse's story
</title>