PParker
PParker

Reputation: 1511

Navigate website and extract phone numbers

I am trying to use Beautiful Soup to scrape illegal phone numbers from a website for multiple countries.

The html code has following structure:

<div class="Table">
<div class="Title"> </div>
<div class="Row">
<div class="Cell">
<div>United Kingdom<br>
<a href="447465832167-UnitedKingdom"><img src="flags/flag-uk.png" alt="SMS - United Kingdom" style="vertical-align: middle;">&nbsp;&nbsp;+447465832167<br>
</a><strong> SMS received:23304<section style="border:none; height: auto; padding: 1px; width: auto; background: #33FF66;"></section></strong></div>
</div>
<div class="Cell">
<div>Germany<br>
<a href="4915902933699-Germany"><img src="flags/german_flag.gif" alt="SMS - Germany" style="vertical-align: middle;">&nbsp;&nbsp;+4915902933699<br>
</a><strong> SMS received:21712<section style="border:none; height: auto; padding: 1px; width: auto; background: #33FF66;"></section></strong></div>
</div>
<div class="Cell">
<div>India<br>
<a href="919532351442-India"><img src="flags/flag-india.png" alt="SMS - India" style="vertical-align: middle;">&nbsp;&nbsp;+919532351442<br>
</a><strong> SMS received:1593<section style="border:none; height: auto; padding: 1px; width: auto; background: #33FF66;"></section></strong></div>
</div>
....
</div>

You can see the different countries (United Kingdom, Germany,..). Each country has a phone number which I am trying to get. This is what I did:

...
soup = bs.BeautifulSoup(source, parser)
mydivs = soup.findAll("div", {"class": "Cell"})

Result:

[<div class="Cell">
<div>United Kingdom<br/>
<a href="447465832167-UnitedKingdom"><img alt="SMS - United Kingdom" src="flags/flag-uk.png" style="vertical-align: middle;"/>  +447465832167<br/>
</a><strong> SMS received:23324<section style="border:none; height: auto; padding: 1px; width: auto; background: #33FF66;"></section></strong></div>
</div>, <div class="Cell">
<div>Germany<br/>
<a href="4915902933699-Germany"><img alt="SMS - Germany" src="flags/german_flag.gif" style="vertical-align: middle;"/>  +4915902933699<br/>
</a><strong> SMS received:21739<section style="border:none; height: auto; padding: 1px; width: auto; background: #33FF66;"></section></strong></div>
</div>, <div class="Cell">
<div>India<br/>
...
]

Question: How can I retrieve the phone numbers from this?

Upvotes: 0

Views: 84

Answers (1)

Rusticus
Rusticus

Reputation: 382

You're almost there, try:

>>> [div.a.text.strip() for div in mydivs]
['+447465832167', '+4915902933699', '+919532351442']

Upvotes: 1

Related Questions