这周接了个小活,发个博客庆祝一下

需要从https://www.mwcbarcelona.com/exhibition/2019-exhibitors/这个网站上将所有的公司照下来(24000+),并在其中找到所有总部位于中国的公司,列成一张列表。
这项任务考察了关于爬虫的诸多要点,从难度上来说还是比较简单的。
难点在于,由于网站并没有列出这些公司到底是哪一个国家的,因此需要自己到网上找信息,而由于这些公司在网站上以英文表述,但是在国内注册是用的是中文,因此在工商局、企查查等平台上查询的时候总是失败。并且很多公司在百度百科上是找不到的。
我也尝试了SEC.gov(U.S. Securities and Exchange Commission)等国外的平台,但由于很多公司没有注册,因此也不能查全。
我突然灵光一现,为什么要那么麻烦呢。经过交流之后,我确定了如下的解决方案
1、进入留在mwc网站上的这些公司的官网(如果有的话)如果其中有中文,则认定这是一家中国公司。
2、查询他们留在mwc网站上的电话,如果归属于中国,就认定他们是中国公司
这样,所有的困难都被规避了,实测下来效果不错。

代码如下
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 09:49:09 2018
@author: Minghua Chen
"""
import requests
import re
import time
import random
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36'}
i=1
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.89 Safari/537.36'}
ans=[]
while True:
if i ==1:
url='https://www.mwcbarcelona.com/exhibition/2019-exhibitors'
else:
url='https://www.mwcbarcelona.com/exhibition/2019-exhibitors/page/%s/'%i
html=requests.get(url,headers=header,verify=False)
m= html.content
print '%s'%i
part=re.findall(u'<div class="listing">(.*?)<p class="list-products">',m,re.S)
for each in part:
k=[]
companyname=re.findall(u'<div class="box-title">(.*?)</div>',each,re.S)
companysite=re.findall(u'<a href="(.*?)" class="listing-item entity"',each,re.S)
k.append(companyname[0])
k.append(companysite[0])
ans.append(k)
'''
f=open('1.txt','a')
f.write(each)
f.write('\n')
f.close()
'''
i=i+1
if i==78:
break
print ans
sol=[]
for i in ans:
try:
url=i[1]
html=requests.get(url,headers=header,verify=False)
m= html.content
if '+86' in m:
sol.append(i[0])
else:
web=re.findall(u'<div class="email-icon"></div>(.*?)="web-site-link" target="_blank">',m,re.S)
for k in web:
print 'try1'
realsite=re.findall(u'<a href="(.*?)" class',k,re.S)
if realsite!=[]:
print 'try2'
url1=realsite[0]
print url1
html1=requests.get(url1,headers=header,verify=False)
m1= html1.content
if '网' in m1:
sol.append(i[0])
else:
pass
else:
pass
print sol
except:
pass
本文介绍了一个使用Python爬虫从mwcbarcelona.com网站中抓取并识别总部位于中国的公司案例。面对网站未提供公司国家信息的挑战,通过检查公司官网语言和电话归属地来判断公司是否为中国公司,实现简单有效的解决方案。

1万+

被折叠的 条评论
为什么被折叠?



