【问题标题】:Given list of websites, search and return information in Python给定网站列表,在 Python 中搜索并返回信息
【发布时间】:2019-08-10 16:52:22
【问题描述】:

我创建了一个函数,它返回给定特定公司名称的 url 列表。我想知道通过这个 url 列表搜索并查找有关该公司是否为另一家公司所有的信息。

示例:“Marketo”公司被 Adob​​e 收购。

我想返回某家公司是否被收购以及被谁收购。

这是我目前所拥有的:

import requests
from googlesearch import search
from bs4 import BeautifulSoup as BS


def get_url(company_name):
    url_list = []
    for url in search(company_name, stop=10):
        url_list.append(url)
    return url_list


test1 = get_url('Marketo')
print(test1[7])


r = requests.get(test1[7])
html = r.text
soup = BS(html, 'lxml')
stuff = soup.find_all('a')


print(stuff)

我是网络抓取的新手,我不知道如何真正搜索每个 URL(假设我可以)并找到我想要的信息。

test1的值如下表:

['https://www.marketo.com/', 'https://www.marketo.com/software/marketing-automation/', 'https://blog.marketo.com/', 'https://www.marketo.com/software/', 'https://www.marketo.com/company/', 'https://www.marketo.com/solutions/pricing/', 'https://www.marketo.com/solutions/', 'https://en.wikipedia.org/wiki/Marketo', 'https://www.linkedin.com/company/marketo', 'https://www.cmswire.com/digital-marketing/what-is-marketo-a-marketers-guide/']

【问题讨论】:

  • 你能给我们test1列表的值吗?
  • 您在维基百科链接中寻找的信息并不容易找到。右边的信息框中没有这个信息,所以你必须使用一些语言处理在文本上找到它
  • 我认为您的要求是不可能的 - 要从网页上抓取信息,您必须知道在该网页上的哪个位置可以找到它。您无法保证这些信息甚至出现在特定公司的网站上——更不用说每个网站上的“统一”位置了。您可能最好寻找一个 API 来获取此类信息 - 例如,我看到对于英国公司,您可以使用 this。不知道其他国家有没有类似的。
  • 提交程序化搜索查询是违反 Google 的Webmaster Guidelinesterms of service 的。对 Google 运行此代码可能会导致 Google 显示来自您 IP 地址的搜索的验证码。

标签: python web-scraping beautifulsoup google-search


【解决方案1】:

您可以从 Crunchbase 等网站找到该信息。

获取步骤如下:

  1. 构建包含目标公司信息的 url。假设您找到包含所需信息的 url,例如:

    url = 'https://www.example.com/infoaboutmycompany.html'

  2. 使用 selenium 获取 html,因为该站点不允许您直接抓取页面。像这样的:

    from selenium import webdriver from bs4 import BeautifulSoup driver = webdriver.Firefox() driver.get(url) html = driver.page_source

  3. 使用 BeautifulSoup 从包含信息的 div 中获取文本。它有一个特定的类,您可以通过查看 html 轻松找到它:

    bsobj = BeautifulSoup(html, 'lxml') res = bsobj.find('div', {'class':'alpha beta gamma'}) res.text.strip()

不到 10 行代码就能搞定。

当然,它可以将您的列表从网址列表更改为公司列表,希望该网站会考虑。对于marketo,它可以工作。

【讨论】:

  • 你能用一个例子来扩展你的答案吗?看到可行的解决方案后,我很乐意接受您的回答。
  • 我不知道这种抓取是否完全公平。可能来自 stackoverflow 工作人员的某个人应该同意明码。无论如何,我会给你每个步骤的编码思路。
  • 我现在才开始看,什么是“alpha beta gamma”,这只是您要查找 html 的东西吗?
  • 我确实收到了一些错误:selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
  • 如果您想获得赏金,如果您可以提供一个示例,我可以在其中复制和粘贴您的代码以了解我需要为我的特定任务修改和更改的内容,这将有所帮助。谢谢。
【解决方案2】:

我想返回某家公司是否被收购以及被谁收购

您可以抓取crunchbase 网站来获取此信息。缺点是您会将搜索限制在他们的网站上。要扩展它,您也许还可以包括其他一些网站。

import requests
from bs4 import BeautifulSoup
import re
while True:
    print()
    organization_name=input('Enter organization_name: ').strip().lower()
    crunchbase_url='https://www.crunchbase.com/organization/'+organization_name
    headers={
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    r=requests.get(crunchbase_url,headers=headers)
    if r.status_code == 404:
        print('This organization is not available\n')
    else:
        soup=BeautifulSoup(r.text,'html.parser')
        overview_h2=soup.find('h2',text=re.compile('Overview'))
        try:
            possible_acquired_by_span=overview_h2.find_next('span',class_='bigValueItemLabelOrData')
            if possible_acquired_by_span.text.strip() == 'Acquired by':
                acquired_by=possible_acquired_by_span.find_next('span',class_='bigValueItemLabelOrData').text.strip()
            else:
                acquired_by=False
        except Exception as e:
                acquired_by=False
                # uncomment below line if you want to see the error
                # print(e)
        if acquired_by:
            print('Acquired By: '+acquired_by+'\n')
        else:
            print('No acquisition information available\n')

    again=input('Do You Want To Continue? ').strip().lower()
    if  again not in ['y','yes']:
        break

样本输出:

Enter organization_name: Marketo
Acquired By: Adobe Systems

Do You Want To Continue? y

Enter organization_name: Facebook
No acquisition information available

Do You Want To Continue? y

Enter organization_name: FakeCompany
This organization is not available

Do You Want To Continue? n

备注

  • 在将其部署到任何商业项目之前,请阅读 crunchbase Terms 并征得他们的同意。

  • 还可以查看crunchbase api - 我认为这将是实现您所要求的合法方式。

【讨论】:

  • 对于“用户代理”部分,我不使用 Linux 机器,所以这适用于 windows 吗?
【解决方案3】:

正如其他答案所提到的,crunchbase 是获取此类信息的好地方,但您需要一个无头浏览器来抓取 crunchbase 比如


如果您使用的是 ubuntu,安装 Selenium 相当容易。 Selenium 需要驱动程序来与所选浏览器交互。例如,Firefox 需要 geckodriver

  • pip 安装硒
  • sudo pip3 install selenium --upgrade

安装最新版本的geckodriver

将驱动程序添加到您的 PATH 以便其他工具可以找到它或在您所有软件都安装的目录中,否则它将引发错误('geckodriver' 可执行文件需要在 PATH 中)

  • mv geckodriver /usr/bin/

代码


from bs4 import BeautifulSoup as BS
from selenium import webdriver


baseurl = "https://www.crunchbase.com/organization/{0}"

query = input('type company name : ').strip().lower()
url = baseurl.format(query)

driver = webdriver.Firefox()
driver.get(url)
html = driver.page_source
soup = BS(html, 'lxml')
acquiredBy = soup.find('div', class_= 'flex-no-grow cb-overflow-ellipsis identifier-label').text


print(acquiredBy)

您还可以使用相同的逻辑获取其他信息,只需检查类/ id 并抓取信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-26
    • 2020-10-25
    • 2012-12-21
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多