成都创新互联网站制作重庆分公司

爬取某数据公司电话以及地址信息的方法

这篇文章将为大家详细讲解有关爬取某数据公司电话以及地址信息的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联专注于黔西网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供黔西营销型网站建设,黔西网站制作、黔西网页设计、黔西网站官网定制、成都微信小程序服务,打造黔西网络公司原创品牌,更为您提供黔西网站排名全网营销落地服务。

准备:selenium+PhatomJS或者selenium+Firefox

我这里直接用的后者selenium+Firefox

思路: 爬取这部分信息的话,代码其实不难,主要包括模拟登陆、获得页面网址以及抓取页面信息。

模拟登陆

网址:https://www.tianyancha.com/login

页面如下:

爬取某数据公司电话以及地址信息的方法

使用selenium模拟登陆代码:

time.sleep(random.random()+1)
browser.get(loginURL)
time.sleep(random.random()+random.randint(2,3))
browser.find_element_by_css_selector('div.title:nth-child(2)').click()
time.sleep(random.uniform(0.5,1))
phone = browser.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(2) > input:nth-child(1)')
phone.send_keys(zhangHao)
time.sleep(random.uniform(0.4,0.9))
password = browser.find_element_by_css_selector('.input-pwd')
password.send_keys(miMa)
click = browser.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(5)')
click.click()
time.sleep(random.uniform(0.5,1)+10)

登录之后页面:

爬取某数据公司电话以及地址信息的方法

关键词对应的页面网址:https://www.tianyancha.com/search?key= + key
这里以“滴滴”为例:https://www.tianyancha.com/search?key=滴滴
页面内容如下:

爬取某数据公司电话以及地址信息的方法

获取公司页面网址
解析滴滴关键词页面HTML,获得每个公司对应得URL。
注意:非会员只能查看前五页的公司信息
代码:

#获取页面数
try:
    pages = soup.find('ul',class_='pagination').find_all('li')[-2].getText().replace('...','')
except:
    pages = 1
finally:
    print('pages:',pages)

def getUid(soup):
    urls = []
    divs = soup.find('div',class_='result-list sv-search-container').find_all('div',class_='search-item sv-search-company')                                                                             
    for div in divs:
        urls.append(div.find('div',class_='header').find('a')['href'])
    return urls

#非会员只能爬前五页
if(int(pages)>5):
    pages = 5

urls = []
for i in range(1,pages+1):
    url = 'https://www.tianyancha.com/search/p' + str(i) + '?key=' + key
    browser.get(url)
    time.sleep(random.uniform(0.6,1)+2)
    soup = BeautifulSoup(browser.page_source,'lxml')
    urls.extend(getUid(soup))

获得企业信息
最后根据企业网页HTML内容,解析获取需要的信息,看页面可以发现这里需要的电话和地址都在最上面就有。

爬取某数据公司电话以及地址信息的方法

获取这部分内容代码:

#这里为了避免意外每次将结果直接写入Excel文件
try:
    for url in urls:
        path = r'C:\Users\liuliang_i\Desktop\tianYanCha.xlsx'
        try:
            df1 = pd.read_excel(path)
        except:
            df1 = pd.DataFrame(columns=['Company','Phone','Address','Url'])
        browser.get(url)
        time.sleep(random.uniform(0.4,0.8)+1)
        soup = BeautifulSoup(browser.page_source,'lxml')
        company = soup.find('div',class_='header').find('h2',class_='name').getText()
        phone = soup.find('div',class_='in-block sup-ie-company-header-child-1').find_all('span')[1].getText()
        address = soup.find('div',class_='auto-folder').find('div').getText()
        df1.loc[df1.shape[0],'Company'] = company
        df1.loc[df1.shape[0]-1,'Phone'] = phone
        df1.loc[df1.shape[0]-1,'Address'] = address
        df1.loc[df1.shape[0]-1,'Url'] = url
        df1.to_excel(path,index=0)
except :
    pass

最终结果如下所示:爬取某数据公司电话以及地址信息的方法

关于爬取某数据公司电话以及地址信息的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页名称:爬取某数据公司电话以及地址信息的方法
标题链接:http://cxhlcq.com/article/ijpohi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部