我正在尝试编写一个脚本,它可以简单地读取和打印特定帐户监视列表上的所有代码。我已经成功地导航到页面,从HTML中打印用户名,现在我想通过使用find()查找它们的位置,然后使用.find_all()查找每个滴答器来打印他遵循的所有代码,但是每次我尝试使用find()命令导航到监视列表代码时,它都返回‘NoneType’。
这是我的代码:
import requests
import xlwt
from xlutils.copy import copy
from xlwt import Workbook
import xlrd
import urllib.request as urllib2
from bs4 import BeautifulSoup
hisPage = ("https://stocktwits.com/GregRieben/watchlist")
page = urllib2.urlopen(hisPage)
soup = BeautifulSoup(page, "html.parser")
his_name = soup.find("span", {"class":"st_33aunZ3 st_31YdEUQ st_8u0ePN3 st_2mehCkH"})
name = his_name.text.strip()
print(name)
watchlist = soup.find("div", {"class":"st_16989tz"})
tickers = watchlist.find_all('span', {"class":"st_1QzH2P8"})
print(type(watchlist))
print(len(watchlist))在这里,我需要高亮显示的值(LSPD.CA),以及之后的所有其他值(它们都设置了完全相同的HTML )。

以下是我的错误:

发布于 2019-08-07 16:49:38
该内容是从api调用中动态添加的(因此在您的请求中不存在到原始url中,其中DOM没有像使用浏览器时那样被更新)。您可以在网络流量中找到监视列表的API调用。它会返回json。你可以从中提取你想要的东西。
import requests
r = requests.get('https://api.stocktwits.com/api/2/watchlists/user/396907.json').json()
tickers = [i['symbol'] for i in r['watchlist']['symbols']]
print(tickers)如果您需要获得用户id来传递给API,那么它会出现在许多地方,以响应您原来的url。我使用regex从脚本标签抓取
import requests, re
p = re.compile(r'subjectUser":{"id":(\d+)')
with requests.Session() as s:
r = s.get('https://stocktwits.com/GregRieben/watchlist')
user_id = p.findall(r.text)[0]
r = s.get('https://api.stocktwits.com/api/2/watchlists/user/' + user_id + '.json').json()
tickers = [i['symbol'] for i in r['watchlist']['symbols']]
print(tickers)https://stackoverflow.com/questions/57398062
复制相似问题