【发布时间】:2019-12-26 08:10:33
【问题描述】:
以下是从网站上抓取数据的代码 - http://www.asmi.com/index.cfm?GPID=14
import requests
from bs4 import BeautifulSoup
import re
for num in range(14, 25):
r = requests.get(f"http://www.asmi.com/index.cfm?GPID={num}")
soup = BeautifulSoup(r.text, 'html.parser')
names = list(dict.fromkeys([' '.join(items.split())
for items in soup(text=re.compile(r'Ltd\s?$'))]))
other = list(dict.fromkeys([items.text
for items in soup.findAll("strong", text=True)]))
Name = names+other
Address = list(dict.fromkeys([items.text
for items in soup(text=re.compile(r'Ltd\s\n\Singapore\s[0-9]{6}'))]))
ZipCode = list(dict.fromkeys([items.text
for items in soup(text=re.compile(r'Singapore\s[0-9]{6}'))]))
Email = list(dict.fromkeys([items.text
for items in soup(text=re.compile(r'\w*\@\.com'))]))
Website = list(dict.fromkeys([items.text
for items in soup(text=re.compile(r'\www\.\w*\.com'))]))
Contact = list(dict.fromkeys([items.text
for items in soup(text=re.compile(r'Contact Person:'))]))
使用上面的代码,我无法获取以下两个信息:
- 正确且完整的名称
- 地址行
页面second page的HTML片段
Cyclect Electrical Engineering Pte Ltd
33 Tuas View Crescent
Singapore 637654
Tel: 6868 6000
Fax: 6863 6260
Email: tan.ey@cyclect.com.sg
Website: www.cyclect.com.sg
Contact Person: Mr Marcus Tan
是
<div><span style="font-size: small;"><span style="font-family: Arial;"> <br>
</span></span><span style="font-size: x-small"><span style="font-family: Arial"><span style="font-size: 10pt"><font color="#333333"><strong><a name="C"></a></strong></font></span><span style="font-size: small;"><font color="#333333"><strong>C</strong></font></span><span style="font-size: small;"><strong>yclect Electrical Engineering Pte Ltd<br>
</strong>33 Tuas View Crescent<br>
Singapore 637654<br>
Tel: 6868 6000<br>
Fax: 6863 6260<br>
Email: <a href="mailto:tan.ey@cyclect.com.sg"><span style="font-size: small;">tan.ey@cyclect.com.sg</span></a><br>
</span><span style="font-size: small;">Website: </span><span style="font-size: small;"><a href="http://www.cyclect.com.sg" target="_blank"><span style="font-size: small;">www.cyclect.com.sg<br>
</span></a></span></span></span><span style="font-size: small;"><span style="font-family: Arial;">Contact Person: Mr Marcus Tan <br>
<br>
</span></span><span style="font-size: small;"><strong><a name="E"></a>Engie Axima Singapore Pte Ltd</strong><br>
</span><span style="font-size: 10pt;">
<div><span style="font-size: small;"><span style="font-family: Arial;">108 Pasir Panjang Road</span></span></div>
<div><span style="font-size: small;"><span style="font-family: Arial;">#05-04 PSA Vista</span></span></div>
<div>
<div><span style="font-size: small;"><span style="font-family: Arial;">Singapore 118535</span></span></div>
<span style="font-size: small;"><span style="font-family: Arial;">Tel: 6514 1226</span><br>
Fax: 6305 9335</span><br>
<span style="font-size: small;">Email: </span><a href="mailto:nur-ain-binte.mohd-amin@engie.com"><span style="font-size: small;">nur-ain-binte.mohd-amin@engie.com</span></a><span style="font-size: small;"><br>
Website: </span><span style="font-size: small;"><a href="http://www.engie-axima.fr" target="_blank"><span style="font-size: small;">www.engie-axima.fr</span></a></span></div>
</span><span style="font-size: small;">Contact Person: Mr Cedric Lucas</span></div>
【问题讨论】:
-
您需要包含相关的 HTML 和想要的结果。 (如果链接断开,或者他们更改了 HTML 的结构,那么您的问题的上下文对于未来的读者来说就会丢失。这就是为什么在问题本身中包含所有信息是必要的。)
标签: python python-3.x web-scraping beautifulsoup