【问题标题】:How can I access the Description of a company from crunchbase using python?如何使用 python 从 crunchbase 访问公司的描述?
【发布时间】:2017-01-16 00:31:28
【问题描述】:

我确实尝试过使用 beautifulSoup,但没有成功。

import urllib2
import tldextract
from BeautifulSoup import BeautifulSoup
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
u2 =  urllib2.urlopen("https://www.crunchbase.com/organization/facebook#/entity")
soup = BeautifulSoup(u2)
access_response_2 = soup.find('dl',class = "definition-list-container")

【问题讨论】:

    标签: python beautifulsoup urllib


    【解决方案1】:

    我认为您无法使用 urllib2 连接和解析 cruchabse 网站。上次我尝试他们抛出 416 HTTP 错误。然后我尝试使用 urllib2 请求设置用户代理、正确的 HTTP 标头和 cookie 值,但它也失败了。

    请改用selenium。 有了它,您将能够连接到 cruchbase 页面并进行解析。连接到目标页面后,使用 selenium 自带的 parser 进行解析,也可以使用 beautifulSoup 来解析

    示例代码:

    from selenium import webdriver
    driver= webdriver.Firefox()
    driver.get('https://www.crunchbase.com/organization/facebook#/entity')
    

    用硒解析

    driver.find_element_by_class_name('definition-list-container')
    

    或用 BeautifulSoup 解析

    soup = BeautifulSoup(driver.page_source, "html.parser")
    soup.find('dl',{ 'class' : "definition-list-container"})
    

    【讨论】:

    • 我收到此错误:>>> driver= webdriver.Firefox() Traceback(最近一次调用最后):文件“”,第 1 行,在 文件“/usr/ local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”,第 140 行,在 init self.service.start() 文件“/usr/local/lib /python2.7/dist-packages/selenium/webdriver/common/service.py”,第 81 行,在 start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException:消息: 'geckodriver' 可执行文件需要在 PATH 中。
    • 你需要正确设置selenium驱动首先从this link下载geckodriver,解压zip文件并在驱动初始化语句中提及下载的驱动路径。例如:driver=webdriver.Firefox(executable_path='E:\\apps\\geckodriver.exe')
    • 我能够解决这个问题。但是,您上面的代码不会从 crunchbase 获取公司的描述。你能帮我修一下吗?谢谢!
    • 我知道。我只是提到了方法。您需要使用 selectors 来获取所需的元素。请阅读有关解析 HTML 的 selenium / beautiful soup 文档。要获取公司描述,您可以使用driver.find_element_by_xpath("//dt[text()='Description:']/following-sibling::dd").text
    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多