收集财富500强公司的信息

2024-04-26 13:57:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从http://fortune.com/fortune500获取公司信息,作为我的论文。当我从链接下载web_文本时,没有用于解析的链接。但是,打开Chrome上的链接会自动进入#1个公司页面。在

有人能帮我解释一下发生了什么事,以及如何从原始网址追踪到公司页面的链接?在


Tags: 文本comweb信息http链接公司页面
1条回答
网友
1楼 · 发布于 2024-04-26 13:57:51

首先,您需要获得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

印刷品:

^{pr2}$

相关问题 更多 >