无法通过scrapy 从网站中提取数据,但可以使用xpath 辅助扩展

叶科普

所以我创建了一个爬虫来从一个站点中提取数据,例如https://www.sportstoto.com.my/result_print.asp?drawNo=5291/21

这是我的代码,

    import scrapy
from totoprintasp.items import Result


def generate_start_urls():
    drawNums = ['5291/21']
    return ['https://www.sportstoto.com.my/result_print.asp?drawNo={}'.format(drawNum) for drawNum in drawNums]


class TotoprintSpider(scrapy.Spider):
    name = 'totoprint'
    allowed_domains = ['www.sportstoto.com.my/result_print.asp']
    start_urls = generate_start_urls()
    download_delay = 3

    def parse(self, response):
        # print(response.body)

        items = []
        # print(response.body)
        for each in response.xpath("/html/body/div/center/table/tbody"):
            item = Result()
            drawDate = each.xpath(
                "tr[2]/td/div/table/tbody/tr/td[1]/span/font/b/text()").extract() 
            drawNo = each.xpath(
                "tr[2]/td/div/table/tbody/tr/td[2]/span/b/font/text()").extract()
            gameType = each.xpath(
                "tr[4]/td/span/font/text()").extract()
            firstPrize = each.xpath(
                "tr[5]/td/table[1]/tbody/tr[2]/td[1]/span/b/font/text()").extract()

            item['drawDate'] = drawDate
            item['drawNo'] = drawNo
            item['gameType'] = gameType
            item['firstPrize'] = firstPrize
            items.append(item)
            yield item

它没有提取任何东西。我正在运行命令,scrapy runspider totoprint.py并设置了值,

FEED_URI = 'results.json'

FEED_FORMAT = 'json'

在我的settings.py文件中

所以结果应该写到json文件中

有趣的是什么都没有出现,也没有得到任何提取物。我尝试了不同的变化,甚至改变.extract().get()

XPath 可以正常工作,因为我已经在我的 chrome 浏览器中的 XPath 帮助程序扩展上尝试过它。

在此处输入图片说明

感谢一些帮助或建议。

穆拉特·德米尔

我重写了你的脚本,但你必须根据你自己的项目重新修复它。这里的问题是你正在寻找 1tbody和他们的 1 个孩子。但是有很多tbody.

据我了解,您希望 gameType 作为列表,而其他人则作为字符串。我得到以下输出:

|------------------|-----------------|----------------------------------------|------------|
| drawDate         | drawNo          | gameType                               | firstPrize |
|------------------|-----------------|----------------------------------------|------------|
| Date:30/05/2021  | DrawNo. 5291/21 | TOTO 4D,TOTO 4D ZODIAC,TOTO 5D,TOTO 6D | 4800       |
|------------------|-----------------|----------------------------------------|------------|

顺便说一句,您不必为每个 URL 执行 for 循环。每个 URL 都一一调用解析。所以这里是脚本:

import scrapy

def generate_start_urls():
    drawNums = ['5291/21']
    return ['https://www.sportstoto.com.my/result_print.asp?drawNo={}'.format(drawNum) for drawNum in drawNums]

class TotoprintSpider(scrapy.Spider):
    name = 'totoprint'
    allowed_domains = ['www.sportstoto.com.my/result_print.asp']
    start_urls = generate_start_urls()
    download_delay = 3
    custom_settings = { 
        "ROBOTSTXT_OBEY":False, #You have to close the robotstxt rule because they are not letting you in.
    }

    def parse(self, response):
        drawDate,drawNo = response.xpath('//*[@class="dataDD"]//text()').extract() #Both have same class so you can scrape them together
        gameType = response.xpath('//*[@class="tit4D"]//text()').extract()
        firstPrize = response.xpath('(//*[@class="dataResultA"])[1]//text()').get() #According to your scrit you want just first price because of that I write the xpath with [1]
        yield {
            'drawDate':drawDate.replace("\t","").replace("\n","").replace("\r",""), #There was some issue about t,n,r I delete simply with replace
            "drawNo":drawNo.replace("\t","").replace("\n","").replace("\r",""),
            "gameType":gameType,
            "firstPrize":firstPrize
        }

我想我写的剧本就是你想要的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用Java语言访问类,但可以使用Kotlin

Java编译无法使用方法参考,但可以使用lambda

Eclipse内容辅助无法识别std :: thread,但可以正确编译

symbolicatecrash无法与dSYM一起使用,但可以使用“ atos”

无法使用命令行连接到MySQL,但可以使用MySQL Workbench

Gulp Watch无法使用导入的文件,但可以使用主文件

扩展使用Javascript array.reduce辅助方法

scrapy下载html页面,但可以使用xpaths或CSS获取数据

由于未知原因,无法使用pipenv安装Django(但可以使用pip)

无法连接到Localdb,但可以使用namedpipe

Web API无法使用GET绑定不可变对象,但可以使用POST绑定

Laravel 5与Blade ...使用jQuery我无法在使用辅助函数route()时设置数据属性,但可以使用相对路径

Tensorflow无法通过变量获得梯度,但可以使用Tensor

发布网站后,表单无法使用,但可以在本地主机上使用

无法将扩展方法添加到.NET Core中的IConfiguration(但可以在其他类上使用)

是否可以使用画布来播放视频,但可以通过动画设置像素?

无法使用PHP将视频上传到Facebook页面,但可以使用curl

我无法使用su命令以root用户身份登录,但可以使用SSH登录

无法从woocommerce扩展中的订单中获取运输方式,但可以通过主题获取

MediaContentProvider无法使用SMS应用程序(但可以使用WhatsApp,电子邮件等)

我无法使用scp复制远程文件,但可以使用ssh登录

我无法使用Behat连接到我的网站(但可以使用浏览器)

无法通过ip访问网站,但可以通过完全限定的域访问

无法扩展“ Ext.panel.Panel”,但可以扩展“ Ext.tab.Panel”

网站无法在IE8中使用,但可以在Google Chrome中使用

DNS无法解析,但可以使用FQDN ping和使用nslookup吗?

无法使用xcodebuild构建项目,但可以使用Xcode.app

无法使用 Visual Studio 访问 Azure SQL 数据库,但可以使用 SSMS

scrapy css 选择器无法从网站中提取数据