我使用BeautifulStoneSoup来解析XML文档并更改一些属性.我注意到它会自动将所有XML标记转换为小写.例如,我的源文件包含<DocData>
BeautifulSoup转换为的元素<docdata>
.这似乎引起了问题,因为我提供修改后的XML文档的程序似乎不接受小写版本.有没有办法防止BeautifulSoup中的这种行为?
所以我正在慢慢地学习Python,并且我正在尝试创建一个简单的函数,它将从在线游戏的高分页面中提取数据.这是我重写为一个函数的其他人的代码(这可能是问题),但是我收到了这个错误.这是代码:
>>> from urllib2 import urlopen
>>> from BeautifulSoup import BeautifulSoup
>>> def create(el):
source = urlopen(el).read()
soup = BeautifulSoup(source)
get_table = soup.find('table', {'id':'mini_player'})
get_rows = get_table.findAll('tr')
text = ''.join(get_rows.findAll(text=True))
data = text.strip()
return data
>>> create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
File "<pyshell#17>", line 6, in create
text = ''.join(get_rows.findAll(text=True))
AttributeError: 'ResultSet' object has no attribute 'findAll'
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我需要以这种格式从文本文件(output.txt)中的表中获取数据:data1; data2; data3; data4; .....
Celkova podlahova plocha bytu; 33m; Vytah; Ano; Nadzemne podlazie; Prizemne podlazie; .....; Forma vlastnictva; Osobne
全部在" 一行 "中,分隔符为" ; "(稍后在csv文件中导出).
我是初学者..帮助,谢谢.
from BeautifulSoup import BeautifulSoup
import urllib2
import codecs
response = urllib2.urlopen('http://www.reality.sk/zakazka/0747-003578/predaj/1-izb-byt/kosice-mestska-cast-sever-sladkovicova-kosice-sever/art-real-1-izb-byt-sladkovicova-ul-kosice-sever')
html = response.read()
soup = BeautifulSoup(html)
tabulka = soup.find("table", {"class" : "detail-char"})
for row in tabulka.findAll('tr'):
col = row.findAll('td')
prvy = col[0].string.strip()
druhy = col[1].string.strip()
record = ([prvy], [druhy])
fl = codecs.open('output.txt', 'wb', 'utf8')
for rec in record:
line = ''
for val …
Run Code Online (Sandbox Code Playgroud) 我有一个XML文档,其内容如下:
<xml>
<web:Web>
<web:Total>4000</web:Total>
<web:Offset>0</web:Offset>
</web:Web>
</xml>
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何使用像python中的BeautifulSoup这样的库来访问它们?
xmlDom.web ["Web"].总计?不起作用?
我试图从网址中提取和下载所有图像.我写了一个剧本
import urllib2
import re
from os.path import basename
from urlparse import urlsplit
url = "http://filmygyan.in/katrina-kaifs-top-10-cutest-pics-gallery/"
urlContent = urllib2.urlopen(url).read()
# HTML image tag: <img src="url" alt="some_text"/>
imgUrls = re.findall('img .*?src="(.*?)"', urlContent)
# download all images
for imgUrl in imgUrls:
try:
imgData = urllib2.urlopen(imgUrl).read()
fileName = basename(urlsplit(imgUrl)[2])
output = open(fileName,'wb')
output.write(imgData)
output.close()
except:
pass
Run Code Online (Sandbox Code Playgroud)
我不想提取这个页面的图像看到这个图像http://i.share.pho.to/1c9884b1_l.jpeg 我只是想获得所有图像而不点击"下一步"按钮我不知道怎么能我在"下一课"课程中得到了所有的照片.我应该在findall中做些什么改变?
我正在使用BeautifulSoup来解析一些网页.
偶尔我会遇到如下所示的"unicode hell"错误:
在TheAtlantic.com上查看本文的来源[ http://www.theatlantic.com/education/archive/2013/10/why-are-hundreds-of-harvard-students-studying-ancient-chinese-philosophy/ 280356 / ]
我们在og:description meta属性中看到了这一点:
<meta property="og:description" content="The professor who teaches Classical Chinese Ethical and Political Theory claims, "This course will change your life."" />
Run Code Online (Sandbox Code Playgroud)
当BeautifulSoup解析它时,我看到:
>>> print repr(description)
u'The professor who teaches\xa0Classical Chinese Ethical and Political Theory claims, "This course will change your life."'
Run Code Online (Sandbox Code Playgroud)
如果我尝试将其编码为UTF-8,就像这样评论建议:https://stackoverflow.com/a/10996267/442650
>>> print repr(description.encode('utf8'))
'The professor who teaches\xc2\xa0Classical Chinese Ethical and Political Theory claims, "This course will change your life."'
Run Code Online (Sandbox Code Playgroud)
就在我认为我的所有unicode问题都得到控制的时候,我仍然不太了解发生了什么,所以我将提出几个问题:
1-为什么BeautifulSoup将转换
为\xa0
[拉丁字符集空格字符]?这个页面上的字符集和标题是UTF-8,我认为BeautifulSoup会为编码提取数据吗?为什么不用它替换<space>
? …
我有一个页面,其源代码中包含一些表:
<table width='100%' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>20</b></td>
</tr>
<tr><
td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
</table>
<table width='361' cellspacing='0' cellpadding='2' class='an'>
<tr>
<td width='35%' align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr>
<tr>
<td align='right'>XXX :</td>
<td><b>XXX</b></td>
</tr> …
Run Code Online (Sandbox Code Playgroud) 我知道我想做的事情很简单,但却让我感到悲痛.我想使用BeautifulSoup从HTML中提取数据.为此,我需要正确使用该.find()
功能.这是我正在使用的HTML:
<div class="audit">
<div class="profile-info">
<img class="profile-pic" src="https://pbs.twimg.com/profile_images/471758097036226560/tLLeiOiL_normal.jpeg" />
<h4>Ed Boon</h4>
<span class="screen-name"><a href="http://www.twitter.com/noobde" target="_blank">@noobde</a></span>
</div>
<div class="followers">
<div class="pie"></div>
<div class="pie-data">
<span class="real number" data-value=73599>73,599</span><span class="real"> Real</span><br />
<span class="fake number" data-value=32452>32,452</span><span class="fake"> Fake</span><br />
<h6>Followers</h6>
</div>
</div>
<div class="score">
<img src="//twitteraudit-prod.s3.amazonaws.com/dist/f977287de6281fe3e1ef36d48d996fb83dd6a876/img/audit-result-good.png" />
<div class="percentage good">
69%
</div>
<h6>Audit score</h6>
Run Code Online (Sandbox Code Playgroud)
我想要的价值73599
来自data-value=73599
,32352
来自data-value=32452
和69%
来自percentage good
.
使用过去的代码和在线示例,这是我到目前为止:
RealValue = soup.find("div", {"class":"real number"})['data-value']
FakeValue = soup.find("audit", {"class":"fake number"})['data-value']
Run Code Online (Sandbox Code Playgroud)
到目前为止两者都没有效果.我不确定如何制作这个发现以便提取69% …
我正在使用Python 3.4中的BeautifulSoup进行一些网络抓取.
现在我在学习过程中遇到了一个问题:我正在尝试从网页上获取一个表行,而我正在使用find_all()来获取它们,但是在表格内部 - 还有更多的表格,其中包含表格行!如何在BeautifulSoup中仅获取标签的顶级/第一级通用或特定元素?
# Retrieves all the row ('tr') tags in table
my_table.find_all('tr')
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这个问题是这个问题的重复(只有那里使用的编程语言是PHP):从html中仅提取第一级段落
我今天在使用BeautifulSoup时遇到了一种非常奇怪的行为.
我们来看一个非常简单的html片段:
<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>
Run Code Online (Sandbox Code Playgroud)
我试图<ix:nonfraction>
用BeautifulSoup 获取标签的内容.
使用该find
方法时一切正常:
from bs4 import BeautifulSoup
html = "<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>"
soup = BeautifulSoup(html, 'lxml') # The parser used here does not matter
soup.find('ix:nonfraction')
>>> <ix:nonfraction>lele</ix:nonfraction>
Run Code Online (Sandbox Code Playgroud)
但是,在尝试使用该find_all
方法时,我希望返回一个包含此单个元素的列表,但事实并非如此!
soup.find_all('ix:nonfraction')
>>> []
Run Code Online (Sandbox Code Playgroud)
事实上,find_all
每当我正在搜索的标签中出现冒号时,似乎都会返回一个空列表.
我已经能够在两台不同的计算机上重现这个问题.
有没有人有解释,更重要的是,有一个解决方法?我需要使用该find_all
方法只是因为我的实际案例要求我在整个html页面上获取所有这些标签.
beautifulsoup ×10
python ×10
html ×2
python-3.x ×2
xml ×2
bs4 ×1
unicode ×1
urllib2 ×1
web-scraping ×1
xml-parsing ×1