如何针对nodejs示例在<a> tag?

时间:2017-12-22 10:09:52

标签: python web-scraping beautifulsoup rel

<a href="#" class="tip" rel="&nbsp;
    Principal Name - S. BALKAR SINGH
    Mobile No. - 8146611008
    Email ID - gsssdhapaiasr@gmail.com
    &nbsp;" style="user-select: text;">View Contact Details<span 
class="caret"></span></a>

Principal name, mobile number and email id are the contents I'm interested in. When I specify soup.find('a', {'class':'tip'}) it gives me only "View Contact Details".

Is there a way to extract contents within rel?

1 个答案:

答案 0 :(得分:0)

rel是属性,因此您必须使用['rel'] - 即。 soup.find('a', {'class':'tip'})['rel']

工作示例

data = '''<a href="#" class="tip" rel="&nbsp;
    Principal Name - S. BALKAR SINGH
    Mobile No. - 8146611008
    Email ID - gsssdhapaiasr@gmail.com
    &nbsp;" style="user-select: text;">View Contact Details<span 
class="caret"></span></a>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

item = soup.find('a', {'class':'tip'})

print('text:', item.text)
print(' rel:', item['rel'])
print(' rel:', ' '.join(item['rel']))

结果:

text: View Contact Details
 rel: ['', 'Principal', 'Name', '-', 'S.', 'BALKAR', 'SINGH', 'Mobile', 'No.', '-', '8146611008', 'Email', 'ID', '-', 'gsssdhapaiasr@gmail.com', '']
 rel:  Principal Name - S. BALKAR SINGH Mobile No. - 8146611008 Email ID - gsssdhapaiasr@gmail.com 
BS的{​​{1}}返回列表,而不是一个字符串,因为Multi-valued attributes

编辑:获取数据表,您必须发送rel请求,其中包含通常将浏览器发送到服务器的所有数据 - 它表示数据形式,它甚至可以是空字符串但是服务器必须接收表单字段。

POST

结果:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0'}

# form fields send to server
params = {
    'SchoolType': '',
    'Dist1': '',    
    'Sch1': '', 
    'SearchString': ''  
}

r = requests.post('http://www.registration.pseb.ac.in/School/Schoollist', headers=headers, data=params)

soup = BeautifulSoup(r.text, 'html.parser')

all_a = soup.find_all('a', {'class':'tip'})

for items in all_a:
    print('text:', item.text)
    print(' rel:', item['rel'])
    print(' rel:', ' '.join(item['rel']))
    print('-----')