2024-04-26 13:57:51 发布
网友
我正试图从http://fortune.com/fortune500获取公司信息,作为我的论文。当我从链接下载web_文本时,没有用于解析的链接。但是,打开Chrome上的链接会自动进入#1个公司页面。在
有人能帮我解释一下发生了什么事,以及如何从原始网址追踪到公司页面的链接?在
首先,您需要获得postid,然后向/data/franchise-list发出请求,然后从第一篇文章中获取url:
postid
/data/franchise-list
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
印刷品:
首先,您需要获得
postid
,然后向/data/franchise-list
发出请求,然后从第一篇文章中获取url:印刷品:
^{pr2}$相关问题 更多 >
编程相关推荐