在 Python 中发送 json 请求后在页面中加载更多链接
Loading more links in a page after sending json requests in Python
我正在解析此 URL 以从具有无限滚动的框之一获取 links。这是发送网站请求以获取下一个 10 links:
的 mo 代码
import requests
from bs4 import BeautifulSoup
import urllib2
import urllib
import extraction
import json
from json2html import *
baseUrl = 'http://www.marketwatch.com/news/headline/getheadlines'
parameters2 = {
'ticker':'XOM',
'countryCode':'US',
'docType':'2007',
'sequence':'6e09aca3-7207-446e-bb8a-db1a4ea6545c',
'messageNumber':'1830',
'count':'10',
'channelName':'',
'topic':' ',
'_':'1479539628362'}
html2 = requests.get(baseUrl, params = parameters2)
html3 = json.loads(html2.text) # array of size 10
在对应的HTML中,有这样一个元素:
<li class="loading">Loading more headlines...</li>
告诉我们向下滚动还有更多的项目要加载,但我不知道如何使用 json 文件来编写一个循环来获取更多的 links。
我的第一次尝试是使用 Beautiful Soup 并编写以下代码来获取 links 和 ids :
url = 'http://www.marketwatch.com/investing/stock/xom'
r = urllib.urlopen(url).read()
soup = BeautifulSoup(r, 'lxml')
pressReleaseBox = soup.find('div', attrs={'id':'prheadlines'})
然后检查是否还有更多 link 需要抓取,获取下一个 json 文件:
loadingMore = pressReleaseBox.find('li',attrs={'class':'loading'})
while loadingMore != None:
# get the links from json file and load more links
我不知道实施评论部分的热点。你有什么想法吗?
我没有义务使用 BeautifulSoup,任何其他工作库都可以。
以下是加载更多 json 文件的方法:
- 获取最后一个 json 文件,提取最后一项中键
UniqueId
的值。
- 如果值看起来像
e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2:8499
- 将
e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2
提取为 sequence
- 将
8499
提取为 messageNumber
- 设
docId
为空
- 如果值看起来像
1222712881
- 设
sequence
为空
- 设
messageNumber
为空
- 将
1222712881
提取为 docId
- 将参数
sequence
、messageNumber
、docId
放入您的参数2。
- 使用
requests.get(baseUrl, params = parameters2)
获取下一个 json 文件。
我正在解析此 URL 以从具有无限滚动的框之一获取 links。这是发送网站请求以获取下一个 10 links:
的 mo 代码import requests
from bs4 import BeautifulSoup
import urllib2
import urllib
import extraction
import json
from json2html import *
baseUrl = 'http://www.marketwatch.com/news/headline/getheadlines'
parameters2 = {
'ticker':'XOM',
'countryCode':'US',
'docType':'2007',
'sequence':'6e09aca3-7207-446e-bb8a-db1a4ea6545c',
'messageNumber':'1830',
'count':'10',
'channelName':'',
'topic':' ',
'_':'1479539628362'}
html2 = requests.get(baseUrl, params = parameters2)
html3 = json.loads(html2.text) # array of size 10
在对应的HTML中,有这样一个元素:
<li class="loading">Loading more headlines...</li>
告诉我们向下滚动还有更多的项目要加载,但我不知道如何使用 json 文件来编写一个循环来获取更多的 links。 我的第一次尝试是使用 Beautiful Soup 并编写以下代码来获取 links 和 ids :
url = 'http://www.marketwatch.com/investing/stock/xom'
r = urllib.urlopen(url).read()
soup = BeautifulSoup(r, 'lxml')
pressReleaseBox = soup.find('div', attrs={'id':'prheadlines'})
然后检查是否还有更多 link 需要抓取,获取下一个 json 文件:
loadingMore = pressReleaseBox.find('li',attrs={'class':'loading'})
while loadingMore != None:
# get the links from json file and load more links
我不知道实施评论部分的热点。你有什么想法吗? 我没有义务使用 BeautifulSoup,任何其他工作库都可以。
以下是加载更多 json 文件的方法:
- 获取最后一个 json 文件,提取最后一项中键
UniqueId
的值。- 如果值看起来像
e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2:8499
- 将
e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2
提取为sequence
- 将
8499
提取为messageNumber
- 设
docId
为空
- 将
- 如果值看起来像
1222712881
- 设
sequence
为空 - 设
messageNumber
为空 - 将
1222712881
提取为docId
- 设
- 如果值看起来像
- 将参数
sequence
、messageNumber
、docId
放入您的参数2。 - 使用
requests.get(baseUrl, params = parameters2)
获取下一个 json 文件。