Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 刮除部分<;李>;从<;ul>;上课?_Python 3.x_Web Scraping_Stock - Fatal编程技术网

Python 3.x 刮除部分<;李>;从<;ul>;上课?

Python 3.x 刮除部分<;李>;从<;ul>;上课?,python-3.x,web-scraping,stock,Python 3.x,Web Scraping,Stock,尝试从“ul级mc列表”中获取每个“li级mc”的“子弹”,以获取我的股票列表 我是Python新手,我想对我的股票投资组合进行一点自动化检查 我有一个文件(mystocks.txt),里面有股票行情(每行一张) 一天一次,我想检查SA是否有任何关于我的股票的消息 url = 'https://seekingalpha.com/dividends/dividend-news' response = requests.get(url) soup = BeautifulSoup(response.t

尝试从“ul级mc列表”中获取每个“li级mc”的“子弹”,以获取我的股票列表

我是Python新手,我想对我的股票投资组合进行一点自动化检查

我有一个文件(mystocks.txt),里面有股票行情(每行一张)

一天一次,我想检查SA是否有任何关于我的股票的消息

url = 'https://seekingalpha.com/dividends/dividend-news'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
for link in soup.find_all('li'):
...
预期产出为:

如果div.bullets包含来自“mystocks.txt”的ticker,则应创建名为“ticket.txt”的文件,并包含“div.bullets”文本


查看下面的实现。我希望它能让你达到目的:

import requests
from bs4 import BeautifulSoup

link = "https://seekingalpha.com/dividends/dividend-news"

#following are the pseudo list of tickers you might wanna check against
for ticker in ['NWTUF','BSL','KRC']:
    res = requests.get(link,headers={'User-Agent':'Mozilla/5.0'})
    soup = BeautifulSoup(res.text,"lxml")

    for item in soup.select(".media-body"):
        #if there is no match, get rid of the content
        if ticker not in item.text:continue

        for elem in item.select(".bullets > ul > li, .bullets > ul > li > a"):
            print(elem.text)
        print("***"*20)
小进度(正在学习),逐行读取添加的文件,但即使票据在页面上,也不会打印divi记录:

import requests
from bs4 import BeautifulSoup

link = "https://seekingalpha.com/dividends/dividend-news"
fileHandler = open ("tickers.txt", "r")

with open ("tickers.txt", "r") as fileHandler:
  for ticker in fileHandler:
    print(ticker.strip())
    res = requests.get(link,headers={'User-Agent':'Mozilla/5.0'})
    soup = BeautifulSoup(res.text,"lxml")

    for item in soup.select(".media-body"):
        #if there is no match, get rid of the content
        if ticker not in item.text:continue

        for elem in item.select(".bullets > ul > li, .bullets > ul > li > a"):
            print(elem.text)
        print("***"*20)

# Close Close
fileHandler.close()
输出看起来像(尝试了所有可能的名称): rpi2:~$./divi.py 主要 TJX 纳斯达克:NWFL 奥驰亚
拉尔夫·劳伦(Ralph Lauren)

您好,感谢您的帮助/建议,不幸的是,对于['NWTUF'、'BSL'、'KRC'中的股票代码,''bs4 import BeautifulSoup link''的导入请求无效:。。。res=requests.get(link,headers={'User-Agent':'Mozilla/5.0'})文件“”,第2行res=requests.get(link,headers={'User-Agent':'Mozilla/5.0'})^indentation错误:预期的缩进块缩进是python编程中最重要和最基本的东西之一,您应该首先学习。但是,如果按原样运行代码,则不会遇到任何错误。我刚才查过了。谢谢。看起来你可以逃脱。子弹李,。子弹a也一样+非常感谢。我将测试它一次,我将在周一回到我的电脑。