不能通过beautifulsoup webscrapping蟒蛇得到标签'相对' [英] Cant get the tag 'rel' via beautifulsoup webscrapping python
问题描述
我想测试一个网站上beautifulsoup4 webscrap code。已经做了大部分,但一个属性信息,由于它的位置是有点棘手,我来完成。
code是这样的:
跨度类=callseller-描述图标>
<一个ID =手机铅级=callseller-描述链接的rel =0501365082HREF =#>显示电话号码< / A>
我想这一点,但不能确定是否其好
尝试:
手机= soup.find('A',{'ID':'手机引线'})
对于在电话:
phone_result = STR(a.get_text('相对')。带()。EN code(UTF-8))
打印手机信息:phone_result
除了StandardError的为e:
phone_result =错误是{0}。格式(五)
打印phone_result
什么是可能我的错误。它有点难以得到有电话号码的相对
信息
I M正的错误是
NavigableString对象有没有属性get_text
找到
没有返回元素的列表,如果你希望所有 A
标签,使用的 find_all
方法。还获得相对
属性,你需要使用获得()
方法或者字典查找。您还可以添加的rel =真
只得到那些一的标签,其中的相对属性。
演示:
-
使用
找到()
>>> soup.find('A',{'ID':'手机导','相对':真})得到('相对')
['0501365082'] -
使用
find_all
:>>>对于在soup.find_all('A',{'ID':'手机导','相对':真}):
...打印(A ['相对'])
...
['0501365082']
要获得所有相对,你可以使用列表COM prehensions
列表 >>> [REL在一个['相对'REL]对于在soup.find_all('A',{'ID':'手机导','相对':真})]
['0501365082']
I am trying to test a beautifulsoup4 webscrap code on a website. Have done most of it but one attribute information due to its location is little tricky for me to accomplish.
Code goes like this:
span class="callseller-description-icon">
<a id="phone-lead" class="callseller-description-link" rel="0501365082" href="#">Show Phone Number</a>
I am trying this but not sure if its okay
try:
phone=soup.find('a',{'id':'phone-lead'})
for a in phone:
phone_result= str(a.get_text('rel').strip().encode("utf-8"))
print "Phone information:", phone_result
except StandardError as e:
phone_result="Error was {0}".format(e)
print phone_result
What is possibly my mistake. It kinda hard to get the rel
information which has phone numbers
The error i m getting is
NavigableString object has no attribute get_text
find
returns the element not a list, if you want all a
tags, use the find_all
method. Also to get the rel
attribute you need to use the .get()
method or dictionary lookup. You can also add rel=True
to get only those "a" tags where with the "rel" attribute.
Demo:
Using
find()
>>> soup.find('a', {'id': 'phone-lead', 'rel': True}).get('rel') ['0501365082']
Using
find_all
:>>> for a in soup.find_all('a', {'id':'phone-lead', 'rel': True}): ... print(a['rel']) ... ['0501365082']
To get a list of all "rel" you can use a list comprehensions
>>> [rel for rel in a['rel'] for a in soup.find_all('a', {'id':'phone-lead', 'rel': True})]
['0501365082']
这篇关于不能通过beautifulsoup webscrapping蟒蛇得到标签'相对'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!