通过从CSV fi中的数据手动创建多个URL来废弃这些URL中的数据

2024-05-13 03:22:50 发布

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

我正试图从多个开始使用相同的scrapy蜘蛛文件的网址获取数据。我的目标是通过更改web地址中的特定ID来创建多个URL,并按ID的顺序运行spider。所有ID都保存在CSV文件中。我身份证的正式名称是CIK。为了简单起见,我在这里放了两个cik(在原始文件中,我有大约19000个cik)。你知道吗

1326801个

320193年

因此,手动创建的网站应如下所示:

https://www.secform4.com/insider-trading/1326801-0.htm

https://www.secform4.com/insider-trading/320193-0.htm

我的问题是:如何导入保存在CSV文件中的cik,命令scrapy spider手动构建Start\u url并按顺序运行创建的url?你知道吗

此外,有些CIK在特定网站上没有数据。如何命令spider忽略手动创建的不可用url?你知道吗

我只是个初学者。如果可能,请建议我在我的代码的具体变化(具体的代码将不胜感激)。先谢谢你。你知道吗

import scrapy
class InsiderSpider(scrapy.Spider):
    name = 'insider'
    cik = 320193
    allowed_domains = ['www.secform4.com']
    start_urls = ['https://www.secform4.com/insider-trading/'+ str(cik) +'-0.htm']

Tags: 文件httpscomidurl顺序www手动
2条回答

可以将所有URL写入起始URL,但这不是最佳做法。你知道吗

使用

class MySpider(Spider):
    name = 'csv'

    def start_requests(self):
        with open('file.csv') as f:
            for line in f:
                if not line.strip():
                    continue
                yield Request(line)

如图所示: How to loop through multiple URLs to scrape from a CSV file in Scrapy? 相反。你知道吗

df = '1326801', '320193'
urls = ['https://www.secform4.com/insider-trading/' + str(i) +'-0.htm' for i in df]
print(urls)
['https://www.secform4.com/insider-trading/1326801-0.htm', 'https://www.secform4.com/insider-trading/320193-0.htm']

相关问题 更多 >