使用python bs4从onclick属性获取值
我无法通过onclick属性进行解析,以仅获取选定的值。下面是onclick属性使用python bs4从onclick属性获取值,python,regex,web-scraping,html-parsing,beautifulsoup,Python,Regex,Web Scraping,Html Parsing,Beautifulsoup,我无法通过onclick属性进行解析,以仅获取选定的值。下面是onclick属性 onclick="try{appendPropertyPosition(this,'B10331465','9941951739','','Dealer','Murugan.N');jsb9onUnloadTracking();jsevt.stopBubble(event);}catch(e){};" 如何从这个onclick属性中仅获取选定的值,例如(phonenumber、、‘Dealer’、‘Name’)。
onclick="try{appendPropertyPosition(this,'B10331465','9941951739','','Dealer','Murugan.N');jsb9onUnloadTracking();jsevt.stopBubble(event);}catch(e){};"
如何从这个onclick属性中仅获取选定的值,例如(phonenumber、、‘Dealer’、‘Name’)。这是我的密码
from bs4 import BeautifulSoup
import urllib2
import re
url="http://www.99acres.com/property-in-velachery-chennai-south-ffid?"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
properties = soup.findAll('a', title=re.compile('Bedroom'))
for eachproperty in properties:
print "http:/"+ eachproperty['href']+",", eachproperty.string, eachproperty['onclick']
更新
我只想从上面提到的onclick
属性中获取一个电话号码,尽管有很多
例如,现在我得到了
Y10765227, 9884877926, 9283183326,, Dealer, Rgmuthu
L10038779, 9551154555, ,, ,
R10831945, 9150000747, 9282109134, 9043728565, ,, ,
B10750123, 9952946340, , Dealer, Bala
R10763559, 9841280752, 9884797013, , Dealer, Senthil
这是我通过使用以下代码得到的
re.findall("'([a-zA-Z0-9,\s]*)'", (a['onclick'] if a else ''))
我试图修改这样一种方式,只有一个电话号码被检索,其余的应该消失。应该是这样的
Y10765227, 9884877926, Dealer, Rgmuthu
L10038779, 9551154555
R10831945, 9150000747
B10750123, 9952946340, Dealer, Bala
R10763559, 9841280752, Dealer, Senthil
我正在尝试使用
re.findall("'([a-zA-Z0-9,\s]*)'", (re.sub(r'([^,]+,[^,]+,)(.*?)([A-Za-z].*)', r'\1\0',a['onclick']) if a else ''))
但这似乎不起作用。您可以使用regex从单击
获取数据:
properties = soup.findAll('a', title=re.compile('Bedroom'))
for eachproperty in properties:
print re.findall("'([a-zA-Z0-9,\s]*)'", eachproperty['onclick'])
印刷品:
['Y10765227', '9884877926, 9283183326', '', 'Dealer', 'Rgmuthu']
['L10038779', '9551154555', ',', ',']
['R10831945', '9150000747, 9282109134, 9043728565', ',', ',']
['B10750123', '9952946340', '', 'Dealer', 'Bala']
['R10763559', '9841280752, 9884797013', '', 'Dealer', 'Senthil']
...
希望有帮助。是的。成功了。但我不想要第一个值,即:“Y10765227”。有什么办法可以把它撕下来吗?只要从findall那里取一片[1:][/code>。