【问题标题】:Scrape info of companies on Fortune 500抓取财富 500 强公司的信息
【发布时间】:2014-10-07 07:36:03
【问题描述】:

我正在尝试从http://fortune.com/fortune500 中获取公司信息以用于我的论文。当我从链接下载 web_text 时,没有用于解析的链接。但是,在 Chrome 上打开链接会自动转到 #1 公司页面。

有人可以帮我解释发生了什么,以及如何从原始 url 跟踪到公司页面的链接吗?

【问题讨论】:

  • 这是一个动态加载的网站。从链接加载文本后,您不会获得所有公司。
  • 感谢 Krono 的评论。我刚发现也是一样。我正在搜索包含公司详细信息的财富 500 强名单。你有什么建议吗?

标签: python web-scraping html-parsing


【解决方案1】:

首先你需要获取postid,然后向/data/franchise-list发起请求,然后获取第一篇文章的url:

import json
import re
from urllib2 import urlopen
from urlparse import urljoin
from bs4 import BeautifulSoup

data = urlopen('http://fortune.com/fortune500/')
soup = BeautifulSoup(data)
postid = next(attr for attr in soup.body['class'] if attr.startswith('postid'))
postid = re.match(r'postid-(\d+)', postid).group(1)

url = "http://fortune.com/data/franchise-list/{postid}/1/".format(postid=postid)
data = json.load(urlopen(url))

resulting_url = urljoin(url, data['articles'][0]['url'])
print resulting_url

打印:

http://fortune.com/fortune500/wal-mart-stores-inc-1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2013-01-25
    • 2019-08-22
    • 1970-01-01
    • 2012-04-01
    • 2019-06-15
    • 2021-02-20
    相关资源
    最近更新 更多